Please Note: This article is written for users of the following Microsoft Excel versions: 2007, 2010, and 2013. 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 July 18, 2015)

6

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

ExcelTips is your source for cost-effective Microsoft Excel training. This tip (818) applies to Microsoft Excel 2007, 2010, and 2013. 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

Removing Extra Paragraph Marks

Tired of having too many paragraph breaks in your document? You can get rid of the extra paragraph marks by using the simple ...

Discover More

Inserting Hyperlinks

Connect your worksheets with other workbooks or with the world of the Internet. The ability to add hyperlinks makes this ...

Discover More

Adding Automatic Time Stamps

Your computer knows the current date and time, and Word provides ways you can get that date and time into your document. ...

Discover More

Save Time and Supercharge Excel! Automate virtually any routine task and save yourself hours, days, maybe even weeks. Then, learn how to make Excel do things you thought were simply impossible! Mastering advanced Excel macros has never been easier. Check out Excel 2010 VBA and Macros today!

More ExcelTips (ribbon)

Transferring Data between Worksheets Using a Macro

Macros can be used for all sorts of data processing needs. One need that is fairly common is the need to move data from one ...

Discover More

Deleting Worksheet Code in a Macro

When creating an application in VBA for others to use, you might want a way for your VBA code to modify or delete other ...

Discover More

Determining the Length of a String

Macros are great for working with strings, and one of the most commonly used string functions is Len. This tip explains how ...

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. 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 9 + 4?

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.