by Allen Wyatt
(last updated June 1, 2016)
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.
The First and Last Word on Word! Bestselling For Dummies author Dan Gookin puts his usual fun and friendly candor back to work to show you how to navigate Word 2013. Spend more time working and less time trying to figure it all out! Check out Word 2013 For Dummies today!
There may be times when you want your macro to print out a list of styles in the document. If so, then you can do it with a ...Discover More
Understanding Word's Object Model and how it relates to macros in VBA.Discover More
One of the easiest ways to quickly access a macro is to assign it to a toolbar button. How you make the assignment depends on ...Discover More
FREE SERVICE: Get tips like this every week in WordTips, a free productivity newsletter. Enter your address and click "Subscribe."