vba 为 XLS、XLSX 和 XLSM 编写的宏之间是否存在兼容性问题?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8077850/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is there a compatibility issue between macros written for XLS, XLSX and XLSM?
提问by kinkajou
Is there a compatibility issue between macros written for XLS, XLSX and XLSM? Will the same macro work for all workbooks?
为 XLS、XLSX 和 XLSM 编写的宏之间是否存在兼容性问题?相同的宏是否适用于所有工作簿?
回答by JMax
There are significant differences between these formats :
这些格式之间存在显着差异:
.XLS
is intended to be used for Excel 2003 and above, so your VBA code needs to be backwards compatiblefor earlier versions of Excel (<2007).XLSX
is the Excel 2007 format that cannot store VBA code..XLSM
or.XLSB
are the Excel 2007 format that allow you to save VBA codewith the workbook. As Sydenam said, the differences between these two is the way the workbook is stored.
.XLS
旨在用于 Excel 2003 及更高版本,因此您的 VBA 代码需要向后兼容早期版本的 Excel (<2007).XLSX
是不能存储 VBA 代码的 Excel 2007 格式。.XLSM
或者.XLSB
是允许您将 VBA 代码与工作簿一起保存的 Excel 2007 格式。正如 Sydenam 所说,这两者之间的区别在于工作簿的存储方式。
In short: .XLSB
is the binary format(equivalent to .XLS
for 2007+ version) whereas .XLSM
is the OOXMLformat.
简而言之:.XLSB
是二进制格式(相当于.XLS
2007+ 版本),而.XLSM
是OOXML格式。
See When should the xlsm or xlsb formats be used?for more information.
请参阅何时应使用 xlsm 或 xlsb 格式?想要查询更多的信息。
Addendum for backward compatibility
向后兼容性附录
I can't see any easy way to tell you how it can be backwards compatible, we can't be thatgeneric. You can see on Ozgridthe new methods and properties that were added in Excel 2007. You can also find heresome tips on how to develop on Excel 2007.
我看不到任何简单的方法来告诉您它如何向后兼容,我们不能那么通用。您可以在Ozgrid 上看到Excel 2007 中添加的新方法和属性。您还可以在此处找到有关如何在 Excel 2007 上进行开发的一些提示。
The Ozgridpage will give you the new elementsof Excel 2007 and then will tell you what you shouldn't use if you wanted to be backwards compatible.
该Ozgrid页面会给你新元素的Excel 2007,然后会告诉你,如果你想成为向后兼容,你不应该使用什么。
回答by JimmyPena
See JMax's response for a list of methods that are unavailable in Excel 2003.
有关 Excel 2003 中不可用的方法列表,请参阅 JMax 的响应。
In general, you should write code for the lowest version of Excel you are willing to support. Ex: if you are supporting Excel 2000-2010, your code should be written for Excel 2000.
通常,您应该为您愿意支持的最低版本的 Excel 编写代码。例如:如果您支持 Excel 2000-2010,您的代码应该是为 Excel 2000 编写的。
Keep in mind, however, that some methods may be deprecated in later versions.
但是请记住,某些方法可能会在以后的版本中被弃用。
For example, I would avoid using FileSearchaltogether (deprecated in Excel 2007) and use Dirinstead.
例如,我会完全避免使用FileSearch(在 Excel 2007 中已弃用)并改用Dir。
Your code should also be testing the local Excel version and using conditional compilation depending on the version.
您的代码还应该测试本地 Excel 版本并根据版本使用条件编译。
For help with conditional compilation, see Using Conditional Compilation.
有关条件编译的帮助,请参阅使用条件编译。
Also remember that XLSX files cannot save macros. So you should never save files in this format if they contain macro code.
还要记住,XLSX 文件不能保存宏。因此,如果文件包含宏代码,则永远不要以这种格式保存文件。
回答by Andrew Hymanman
Excel uses Visual Basic for Applications (VBA)for the macros. The primary difference between the different file types is how data is stored (edit & importantXLSX are unable to save Macros, of the formats in the question XLSM and XLS can save macros in the workbook). It is excel and VBA that handle the macros, so it works no problem between all three of those file formats.
Excel 使用Visual Basic for Applications (VBA)作为宏。不同文件类型之间的主要区别在于数据的存储方式(编辑和重要的XLSX 无法保存宏,问题中的格式 XLSM 和 XLS 可以将宏保存在工作簿中)。处理宏的是 excel 和 VBA,因此在所有这三种文件格式之间都没有问题。