Making Macros Run Faster

Written by Allen Wyatt (last updated November 10, 2018)
This tip applies to Word 97, 2000, 2002, and 2003


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.

Note:

If you would like to know how to use the macros described on this page (or on any other page on the WordTips sites), I've prepared a special page that includes helpful information. Click here to open that special page in a new browser tab.

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.

Author Bio

Allen Wyatt

With more than 50 non-fiction books and numerous magazine articles to his credit, Allen Wyatt is an internationally recognized author. He is president of Sharon Parq Associates, a computer and publishing services company. ...

MORE FROM ALLEN

Automatic AutoCorrect Exceptions for Beginning Sentences

When automatically capitalizing the beginning of sentences, Word relies on how you historically have done your typing. ...

Discover More

Combining Cell Contents

Excel allows you to easily combine text together. Interestingly, it provides two ways you can perform such combinations. ...

Discover More

How Excel Treats Disk Files

Workbooks are loaded from disk files, but workbooks aren't the only type of files that Excel can load. This tip provides ...

Discover More

Create Custom Apps with VBA! Discover how to extend the capabilities of Office 2013 (Word, Excel, PowerPoint, Outlook, and Access) with VBA programming, using it for writing macros, automating Office applications, and creating custom applications. Check out Mastering VBA for Office 2013 today!

More WordTips (menu)

Printing Styles in a Macro

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 ...

Discover More

Highlight Words from a Word List

Do you need to highlight certain words in a document, and aren't quite sure how to go about it? Using the techniques ...

Discover More

Offering Options in a Macro

When creating macros, you often need to offer a series of choices to a user. This tip demonstrates how easy it is to ...

Discover More
Subscribe

FREE SERVICE: Get tips like this every week in WordTips, a free productivity newsletter. Enter your address and click "Subscribe."

View most recent newsletter.

Comments

If you would like to add an image to your comment (not an avatar, but an image to help in making the point of your comment), include the characters [{fig}] (all 7 characters, in the sequence shown) in your comment text. You’ll be prompted to upload your image when you submit the comment. Maximum image size is 6Mpixels. Images larger than 600px wide or 1000px tall will be reduced. Up to three images may be included in a comment. All images are subject to review. Commenting privileges may be curtailed if inappropriate images are posted.

What is 1 + 1?

There are currently no comments for this tip. (Be the first to leave your comment—just use the simple form above!)


This Site

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.

Videos
Subscribe

FREE SERVICE: Get tips like this every week in WordTips, a free productivity newsletter. Enter your address and click "Subscribe."

(Your e-mail address is not shared with anyone, ever.)

View the most recent newsletter.