Please Note: This article is written for users of the following Microsoft Excel versions: 2007, 2010, 2013, 2016, 2019, and 2021. 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 May 8, 2024)
This tip applies to Excel 2007, 2010, 2013, 2016, 2019, and 2021


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 2021. 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

Retrieving Drive Statistics

Need to gather some information about the drives on a system? It can be pretty easy to do using a macro, as shown in this ...

Discover More

Displaying Path Names in the Menu Bar

Want a quick way to see the full path name associated with a workbook? You can add a tool to the menu bar that displays ...

Discover More

Setting a Document Naming Convention

Want your document file names to follow a specific naming convention? Word doesn't provide a direct way to set up your ...

Discover More

Excel Smarts for Beginners! Featuring the friendly and trusted For Dummies style, this popular guide shows beginners how to get up and running with Excel while also helping more experienced users get comfortable with the newest features. Check out Excel 2013 For Dummies today!

More ExcelTips (ribbon)

Understanding the If ... End If Structure

One of the most basic of programming structures is the conditional structure: If ... End If. This tip explains how this ...

Discover More

Finding Columns of a Certain Width

If you need to find out how many columns are set to be a specific width, you'll need a macro to help determine the info. ...

Discover More

Selecting Visible Cells in a Macro

Many times you need to select just the visible cells before taking some action. It is helpful to know how to make this ...

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 two more than 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.