Please Note: This article is written for users of the following Microsoft Excel versions: 2007, 2010, 2013, 2016, 2019, and Excel in Microsoft 365. 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

Written by Allen Wyatt (last updated October 3, 2020)
This tip applies to Excel 2007, 2010, 2013, 2016, 2019, and Excel in Microsoft 365


4

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 is to make sure 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

One reader suggested giving focus to another application while the macro is running in the Excel worksheet. For instance, open up Notepad and make that window active. The macro should speed up considerably when Excel isn't active anymore. Another option to try is to move the mouse pointer down to the taskbar area instead of having it on the active Excel worksheet.

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, 2016, 2019, and Excel in Microsoft 365. 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

Header and Footer Formatting Codes

When creating headers and footers in an Excel worksheet, you can use special codes to add or format information. This tip ...

Discover More

Hiding Individual Cells

Hiding information in one or more cells can be a challenge. This tip presents several different techniques that can help ...

Discover More

Hyphenating a Selection

Word provides a hyphenation tool that can help you hyphenate words within a document. If you want to apply hyphenation to ...

Discover More

Program Successfully in Excel! John Walkenbach's name is synonymous with excellence in deciphering complex technical topics. With this comprehensive guide, "Mr. Spreadsheet" shows how to maximize your Excel experience using professional spreadsheet application development tips from his own personal bookshelf. Check out Excel 2013 Power Programming with VBA today!

More ExcelTips (ribbon)

Determining if Calculation is Necessary

When processing a worksheet with a macro, it may be helpful to periodically recalculate the worksheet. Wouldn't it be ...

Discover More

Disabling Shift Key Use when Opening a Workbook

Open up a workbook, and Excel normally runs the macros associated with that workbook. You can disable the automatic ...

Discover More

Generating Unique, Sequential Names

Do you need to create a number of words or phrases where you only alter a few letters in each one? If the alterations ...

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?

2020-10-05 04:00:41

DaveS

@JW @CB
You raise an important point. If temporarily changing Application settings such as these in a macro and resetting them at the end of the macro, it's important to include an error trap to handle the resetting in the event of crashing out.


2020-10-04 12:52:24

J. Woolley

@Craig Buback
Craig, I was wrong. I understand that you were trying to get the person to work through the issue himself.


2020-10-03 10:37:03

J. Woolley

@Craig Buback
Instead of asking a question like this, why don't you test it yourself then tell us what you learned?


2020-10-03 07:45:21

Craig Buback

Re; Macros Running Slowly
If Screen Updating is turned off during a portion of a macro and that section bombs, will the screen be updated to the point where the macro bombed? IOW, if a loop is running and putting data in a table row 1, row 2, row 3 and bombs at this point where it is trying to data in row 3, say because you made a mistake and it can't calculate data for row 3. During troubleshooting, When you look at the table, will row 1 and row 2 have data in them?


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.