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.
Written by Allen Wyatt (last updated May 8, 2024)
This tip applies to Excel 2007, 2010, 2013, 2016, 2019, and Excel in Microsoft 365
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:
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.
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!
If you are getting an error when you try to create an event handler, it could be related to a long-known bug in Excel. ...
Discover MoreNormally a macro is only calculated when you specifically tell Excel to calculate it. Some macros need to be calculated ...
Discover MoreThe Text-to-Columns tool is an extremely powerful feature that allows you to divide data in a variety of ways. Excel even ...
Discover MoreFREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
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?
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.
FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
Copyright © 2024 Sharon Parq Associates, Inc.
Comments