
Tips.Net > WordTips Home > Macros > Making Macros Run Faster, Take Two
Summary: A simple way to make your Macros run faster. Part II. (This tip works with Microsoft Word 97, Word 2000, Word 2002, and Word 2003.)
Last week's tip about making macros run faster was met with a good number of comments, questions, and caveats by WordTips readers. Some readers recognized the technique described--putting multiple statements on the same line and separating them with colons--as something that was often done in older versions of the BASIC programming language. Several readers also asked for additional information regarding the technique and how it is implemented "behind the scenes" in VBA. (Even though this macro specifically refers to VBA, the concepts it discusses and the techniques resulting therefrom are also applicable to WordBasic macros.)
When you create a macro in VBA, it is "pseudocompiled" or "tokenized" when you save the macro or when you save the template in which the macro is stored. Instead of the human-readable form that is visible in the VBA Editor, each keyword or directive is converted internally to a three-byte "token" which is easily processed by VBA. Variable names, comments, and literal string values--none of which are keywords or directives--are not modified and are saved "as is." Even though VBA tokenizes the keywords and directives, it does not "compress" any of the lines in the macro; each line is maintained as it is, with the carriage return at the end of the line intact.
When the macro is executed, the VBA processor fetches a single line of the macro at a time. The tokens on the line are interpreted and executed, in the order that they appear on the line. When one line is completed, the next line is fetched and executed, and so on, through the end of the macro. When you combine multiple statements on a single line and separate them by colons, the idea is to reduce the number of fetch cycles that the processor must go through. This is where the speedup comes into play; less fetching means less time spent doing "overhead" and more time spent actually executing the macro itself.
It should be obvious that when you combine statements on a single line, the resulting macro can be much more difficult for you, as a human, to read. Thus, the "optimize" or "don't optimize" decision involves a tradeoff--harder to read code vs. faster code. It has been this same way since the early days of BASIC, long before Word was even dreamed of.
You should understand that the time saved by combining statements on a single line is miniscule, particularly with today's fast computers. Thus, it doesn't make much sense to combine statements in which the portion of code will be run only once. The real savings comes into play if the code will be repeated hundreds or thousands of times during the course of the macro. The time savings realized from each iteration of the code may be small, but the cumulative time savings may be quite attractive.
If you decide to "compress" your code by combining multiple statements and keywords on a single line, there are a few things to keep in mind. First of all, the maximum line length is 255 characters. Also, Sub statements, which are used to start macros, must appear on their own line, as must With and End With statements. (In WordBasic you can concatenate the Sub statement with other statements, but if you later convert the macro to VBA, you must manually break the line in two.)
Astute readers probably picked up another idea by the information presented earlier in this tip. Since the "pseudocompiler" does not compress variable names, literals, or comments, you can somewhat speed up your macro by reducing the length of variable names, the use of literals, and the use of comments. Again, the tradeoff of such a decision is in readability. When maintaining your macro at a later date, shorter variable names are more cryptic, and a lack of comments can be counterproductive. Thus, you must be the judge on whether the tradeoff is worth it.
There is one other interesting thing to note--if you do any concatenation of lines in your macros, you will obviously want to do some testing to make sure that they still work as you intend. As you are stepping through the macros (in the VBA Editor), notice that the Step Into command (F8) still only executes one statement at a time, not a whole line at a time. This behavior is specifically for the interactive development environment, and does not reflect how the VBA engine behaves when the macro is running full-speed. At run-time, the macro is executed as described above--a full line at a time.
Tip #1644 applies to Microsoft Word versions: 97 2000 2002 2003
Ultimate Library! The most amazing collection ever offered in the history of WordTips places every tip—present and historical—at your fingertips. Check out the WordTips Ten-Year Library.
Discover how to use Word's mail merge tool to create your own custom documents in just minutes. Great e-book answers all your questions. (more information...)
Ask a Word Question
Make a Comment
Beauty Tips
Car Tips
Cleaning Tips
College Tips
Cooking Tips
Excel2007 Tips
ExcelTips
Family Tips
Gardening Tips
Health Tips
Home Tips
Money Tips
Pet Tips
Word2007 Tips
WordTips