調試 編譯 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

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