Please Note: This article is written for users of the following Microsoft Excel versions: 97, 2000, 2002, and 2003. If you are using a later version (Excel 2007 or later), this tip may not work for you. For a version of this tip written specifically for later versions of Excel, click here: Macro Runs Slowly, but Steps Quickly.

Macro Runs Slowly, but Steps Quickly

Written by Allen Wyatt (last updated January 11, 2021)
This tip applies to Excel 97, 2000, 2002, and 2003


1

Fredric wrote about a problem he was having with a macro. When he is running the macro in the VB Editor using F8 (stepping through the macro), it completes in just a few minutes. When he runs the macro outright, it seems to take forever to run, often taking 20 minutes or more to execute. Even though Fredric's workbook is large (46 MB), the time differential between the two methods of running is bothersome.

Problems like this can be baffling, and they often take some heavy-duty analysis in order to figure out. A good place to start is to add some "timer code" in your macro. Add a small routine that saves a time value and another routine that compares that saved value to the current time and displays the difference. At the beginning of a section of code you want to analyze, you call the first routine (which saves the start time) and then at the end of the section of code you call the second routine. In that way, you can determine which portions of your code are taking the longest time to execute. These are the code sections you then focus on, so you can figure out what they are doing that is taking so long.

Another thing to make sure is that you add these two lines at the beginning of your macro:

Application.ScreenUpdating = False
Application.EnableEvents = False

These turn off screen updating, which can slow down a running macro, and disable events. This last line is included so that changes done by the macro in your worksheet won't trigger Excel's recalculation routines. If your macro is making a lot of changes in the data in the worksheet, and a full recalculation is triggered after each change, then with such a large workbook, lots and lots of time can be spent just doing the recalc. At the end of your macro, you reverse the effect of the two lines you added:

Application.EnableEvents = True
Application.ScreenUpdating = True

Note:

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

ExcelTips is your source for cost-effective Microsoft Excel training. This tip (2436) applies to Microsoft Excel 97, 2000, 2002, and 2003. You can find a version of this tip for the ribbon interface of Excel (Excel 2007 and later) here: Macro Runs Slowly, but Steps Quickly.

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

Continuous Formatting for Footnotes

If you've got a lot of short footnotes in a document, you might be looking for a way to save space by "crunching up" the ...

Discover More

Automatic Page Numbers across Multiple Documents

Word allows you to specify the starting page number for a document, which comes in handy if you have multiple documents ...

Discover More

Discovering Printer Drift

How accurate is your printer when it comes to placing information on the printed page? The simple technique described in ...

Discover More

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!

More ExcelTips (menu)

Reversing Names In Place

Do you want a way to reverse names within a cell, making them "last, first" instead of "first last?" Here's a handy macro ...

Discover More

Automatically Opening Macro Workbooks when Using a Shortcut Key

Click a button on a toolbar and Excel will go so far as to open a another workbook in order to run a macro associated ...

Discover More

Creating a Directory in a Macro

One of the things you can do with macros is to work with disk files. As you do so, you may have a need to create a new ...

Discover More
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, 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?

2016-06-28 06:27:28

Drazen

I had the same problem as Fredric. The macro executed almost 30 (thirty) times faster when I was using “Step Over” in the VBA debugger. Usual recommendations to disable events and screen updating did not help a bit. Then I noticed that slowdown goes away whenever main Excel window was not the active one. For example, I just need to open another application, any other, even Notepad and give it a focus. The macro would catch the speed instantly. Furthermore, moving mouse pointer down to the task bar area produced the same result. I also noticed that, during macro execution, whenever mouse pointer was on the active Excel sheet, it would start to flicker. The mouse pointer would constantly switch between “default” and “wait” shape. So, my solution is to prevent mouse pointer flicker by doing Application.Cursor = xlWait at the beginning of the macro and then reverse this by doing Application.Cursor = xlDefault at the end of the macro. Voila!


This Site

Got a version of Excel that uses the menu interface (Excel 97, Excel 2000, Excel 2002, or Excel 2003)? This site is for you! If you use a later version of Excel, visit our ExcelTips site focusing on the ribbon interface.

Newest Tips
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, 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.