by Allen Wyatt
(last updated November 10, 2018)
Macros, once they are running right, can make our life easier. (If they aren't running right, life is definitely not easy.) If you have a macro that does a lot of work for you, you may be interested in trying to make it run faster.
The first task to do, of course, is to examine the macro and see if your algorithms (the way you decided to do things) are as efficient as they can be. Once you are satisfied that they are, you can also speed up your macros by simply putting multiple statements on the same line. Consider the following simple macro, which finds the previous occurrence of something in a document:
Sub PrevFind() With Selection.Find .ClearFormatting .Forward = False .Wrap = wdFindAsk .Execute End With End Sub
This macro is very straightforward. When you save the macro, the statements it contains are parsed down to three-byte tokens that can be run more efficiently by VBA. (You never see the tokens; you only see the macro-language equivalent of the tokens.) When the macro is running, the VBA command processor grabs one line at a time and executes all the tokens on that line. Thus, in the foregoing there are eight lines that are individually fetched and executed by the processor.
Now consider the following version of the same macro:
Sub PrevFind() With Selection.Find .ClearFormatting: .Forward = False: .Wrap = wdFindAsk: .Execute End With: End Sub
This version seems much shorter because individual lines have been concatenated and separated with a colon. This decreases the readability of the macro, but it increases the speed at which it will execute. Why? Because the VBA command processor still grabs a line at a time to process. In this instance, there are only four lines to grab. Less "grabbing time" means that execution is faster. The third line of this macro, which used to occupy four lines, is now processed as a single line.
Putting multiple statements on the same line does decrease the readability of a macro. Depending on your needs, however, this could be a fair tradeoff for the speed increase you may achieve. You will need to try this technique in your own macros to see if it makes sense for you.
WordTips is your source for cost-effective Microsoft Word training. (Microsoft Word is the most popular word processing software in the world.) This tip (1640) applies to Microsoft Word 97, 2000, 2002, and 2003.
Comprehensive VBA Guide Visual Basic for Applications (VBA) is the language used for writing macros in all Office programs. This complete guide shows both professionals and novices how to master VBA in order to customize the entire Office suite for their needs. Check out Mastering VBA for Office 2010 today!
When creating a macro that extensively processes a document, you may need to periodically force Word to repaginate the ...Discover More
The names you use for macros can affect what you see when you add those macros to a toolbar. This tip explains how you ...Discover More
When creating macros, you often need to convert a text string that contains numbers into actual numeric values. You do ...Discover More
FREE SERVICE: Get tips like this every week in WordTips, a free productivity newsletter. Enter your address and click "Subscribe."
Got a version of Word that uses the menu interface (Word 97, Word 2000, Word 2002, or Word 2003)? This site is for you! If you use a later version of Word, visit our WordTips site focusing on the ribbon interface.