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

Macro Runs Slowly, but Steps Quickly

by Allen Wyatt
(last updated October 11, 2019)

8

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

You may also want to turn off automatic calculation while your macro is running. Doing so makes sure that Excel doesn't try to calculate intermediate results while the macro is moving things around or otherwise working with data. To turn off automatic calculation, use this line at the beginning of your macro:

Application.Calculation = xlCalculationManual

It is a good idea to turn off automatic calculation in a macro only if your macro doesn't rely on calculated information in the worksheet. If you do turn it off, you can later turn automatic calculation back on by placing the following line near the end of your macro:

Application.Calculation = xlCalculationAutomatic

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 (818) applies to Microsoft Excel 2007, 2010, 2013, and 2016. You can find a version of this tip for the older menu interface of Excel 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

Quickly Moving Text with the Mouse

Drag-and-drop editing is a handy feature when you love to use the mouse. There are two ways you can move text using the ...

Discover More

Erasing Table Lines

When creating tables, Word provides a handy tool that you can use. Once the table is in place, you can use the table ...

Discover More

Using Named Ranges in a Macro

Named ranges are a great capability provided by Excel. You can define all sorts of named ranges in a workbook, but how do ...

Discover More

Professional Development Guidance! Four world-class developers offer start-to-finish guidance for building powerful, robust, and secure applications with Excel. The authors show how to consistently make the right design decisions and make the most of Excel's powerful features. Check out Professional Excel Development today!

More ExcelTips (ribbon)

Inserting Worksheet Values with a Macro

Macros are often used to process information in a worksheet. You may need your macro to change the values stored in ...

Discover More

Store Common Macros in the Personal Macro Workbook

Want your macros to be available regardless of the workbook on which you are working? Here's how to store them in the ...

Discover More

Macro Fails after Filter

When developing a macro that others may use, you might want to test it out to make sure it works properly if a filter is ...

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}] 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 3 - 2?

2019-10-11 08:47:32

Ann

Game changer! Between Allen's tips and Drazen's in the comments, I have just cut out about two minutes of waiting every morning for a very simple set of macros -- one which unhides rows on every sheet in a workbook so I can add the new day's data, etc., and one that re-hides them so it's scaled down to what the boss needs to look at. And I'll be copying and pasting those lines of code again and again. Best tip (for me) in a while!


2017-08-23 12:27:48

Arjun

Those 6 lines made a huge difference to my macro run times. I had a set of 15 macros that collectively took a little over 40 minutes to run. After the addition of these lines, it's down to just 8 minutes now! Thanks a ton!


2017-03-01 06:28:50

John Little

Drazen - THANK YOU !! This has been confusing me all morning. I had the exact same observations as you - but your solution works. Would be great to know why.


2016-06-28 06:28:37

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!


2015-07-29 11:36:16

Nick from london

I had a macro that took time in Outlook and added a progress bar.

Microsoft has an example at https://support.microsoft.com/en-us/kb/211736

There are plenty of others.


2015-07-20 11:06:13

David Gardner

After you turn automatic calculation back on, you may need to ensure all formulas recalculate by adding Calculate after Application.Calculation = xlCalculationAutomatic; add it after John Finley's style of code. Like:
With Application
.ScreenUpdating = True
.Calculation = xlCalculationAutomatic
.EnableEvents = True
End With
Calculate


2015-07-20 09:57:14

Bob

"Neater" code is subjective.

Personally I don't like using the With syntax. Yes, it makes the inner commands shorter that reference the With object, but it also makes them a bit harder to read. It also adds 2 lines to your code?


2015-07-19 09:01:43

John Finley

WJust a style thing, but using With statement makes a snippet that can be copied and modified at beginning and end of code. Like:
With Application
.ScreenUpdating = False
.Calculation = xlCalculationManual
.EnableEvents = False
End with
Same outcome, just a little neater code.


This Site

Got a version of Excel that uses the ribbon interface (Excel 2007 or later)? This site is for you! If you use an earlier version of Excel, visit our ExcelTips site focusing on the menu 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.