调试 编译 VBA Project的作用(执行语法检查)

VBA code is never stored as the plain text that you type in to the editor. Input is immediately converted to platform- and version-independent byte codes called OpCodes. These OpCodes are converted by the editor to the text you see on the screen. When you compile the project, the compiler translates these OpCodes to platform- and version-specific codes called ExCodes. When you run the code, the runtime reads the ExCodes and executes actual machine code on behalf of the project based on the ExCodes.  This whole process is similar in principle to how Java and the Java Virtual Machine work.

If you were to export all your VBA code to text files and then remove all the modules and then re-import the code from the text files back into VBA (which is exactly what Rob Bovey's Code Cleaner does), you'll see a decrease in file size. This is because the ExCodes were purged and have not yet been recreated. Then, if you compile the project, the file size will increase because now it stores the ExCodes in addition to the OpCodes.

You really never need to compile the code. VBA will automatically do it when necessary. However, the Compile command also does syntax checking, which is its only real practical purpose.


VBA代码永远不会存储为您输入到编辑器中的纯文本。输入立即被转换为平台和版本无关的字节码,称为操作码。编辑器将这些操作码转换为您在屏幕上看到的文本。在编译项目时,编译器将这些操作码转换为平台和版本特定的代码,称为excode。当您运行代码时,运行时将读取excode并基于excode代表项目执行实际的机器码。这整个过程在原则上类似于Java和Java虚拟机的工作方式。

如果您要将所有VBA代码导出到文本文件中,然后删除所有模块,然后将文本文件中的代码重新导入到VBA中(这正是Rob Bovey的代码清理器所做的),您将看到文件大小的减少。这是因为清除了excode,并且还没有重新创建excode。然后,如果编译项目,文件大小将增加,因为现在除了操作码之外还存储excode。

你真的不需要编译代码。VBA会在需要的时候自动执行。然而,Compile命令还执行语法检查,这是它唯一的实际用途。

http://msgroups.net/excel.programming/what-does-compile-vbaproject-do/48408

Chip Pearson
Microsoft Most Valuable Professional

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章