Written by Allen Wyatt (last updated January 11, 2021)
This tip applies to Excel 97, 2000, 2002, and 2003
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
Note:
ExcelTips is your source for cost-effective Microsoft Excel training. This tip (2436) applies to Microsoft Excel 97, 2000, 2002, and 2003. You can find a version of this tip for the ribbon interface of Excel (Excel 2007 and later) here: Macro Runs Slowly, but Steps Quickly.
Solve Real Business Problems Master business modeling and analysis techniques with Excel and transform data into bottom-line results. This hands-on, scenario-focused guide shows you how to use the latest Excel tools to integrate data from multiple tables. Check out Microsoft Excel 2013 Data Analysis and Business Modeling today!
When sharing workbooks with others, you may find that the macros in those workbooks may not work as you expect. This tip ...
Discover MoreMacros are often used to process information in a worksheet. You may need your macro to change the values stored in ...
Discover MoreMacros are often used to process information in a workbook. If your macro makes changes in what is selected in the ...
Discover MoreFREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
2016-06-28 06:27:28
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!
Got a version of Excel that uses the menu interface (Excel 97, Excel 2000, Excel 2002, or Excel 2003)? This site is for you! If you use a later version of Excel, visit our ExcelTips site focusing on the ribbon interface.
FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
Copyright © 2023 Sharon Parq Associates, Inc.
Comments