Making Macros Run Faster

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:

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.

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.

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. Learn more about Allen...

MORE FROM ALLEN

Shortcut for AutoCorrect Dialog Box

There is no built-in keyboard shortcut that will display the AutoCorrect dialog box. This doesn't mean that there aren't a ...

Do More in Less Time! Are you ready to harness the full power of Word 2013 to create professional documents? In this comprehensive guide you'll learn the skills and techniques for efficiently building the documents you need for your professional and your personal life. Check out Word 2013 In Depth today!

More WordTips (menu)

Automatically Inserting Tomorrow's Date

Do you routinely need to work with tomorrow's date? Why not create a template that automatically adds tomorrow's date to any ...

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

Source

(gif, jpeg or png only, 5MB maximum file size)

Source

(gif, jpeg or png only, 5MB maximum file size)

Source

(gif, jpeg or png only, 5MB maximum file size)

Notify me about new comments ONLY FOR THIS TIP

Notify me about new comments ANYWHERE ON THIS SITE

Hide my email address

What is 3 + 3?

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