Written by Allen Wyatt (last updated August 14, 2021)
This tip applies to Excel 97, 2000, 2002, and 2003
I was talking with a coworker a while back, and he described a problem he was having with a macro running on a very powerful computer system. It seems that the macro would always crash and freeze the machine, always with an out of memory error. My coworker thought that if the macro could run in the background, that it wouldn't use so many resources and wouldn't crash.
I knew that the proposed solution wouldn't necessarily solve the problem, however. While it might free up some resources, the system would still eventually run out of memory. Why? Because out-of-memory problems are typically due to coding problems in the original macro. "Memory leaks" (which lead to the out-of-memory condition) can be caused by any number of problems in macro code.
The best solution is a round of late-night debugging, stepping through macro code and analyzing where the problem is creeping in. Look for the most obvious (but easily overlooked) problems first, such as infinite loops. If the macro is doing a lot of repetitive processing (looping through worksheets), then make sure you're releasing all the memory you declare for your macro. For instance, for every SET statement you use, you should have a corresponding statement setting the object to NOTHING, and those statements should be within the loop.
If you can step through the macros without them failing, then there is a good chance the problem lies in some sort of timing issue in the threads—a timing issue that only shows up, of course, when the macro is running full-tilt on its own. If you suspect this is the problem, then perhaps a re-sequencing of the events in the macro can work around it. If the macro uses DDE, you should be aware that Microsoft is recommending the use of OLE automation instead of DDE. Timing problems are fairly common with DDE, and Microsoft now considers it obsolete and too flaky to fix (meaning they won't support it). In VBA, multiple calls to a subroutine can also cause memory leaks, and such subroutines have to be rewritten as user-defined functions.
You should also make sure that all variable references are fully declared. I've seen some reports of macros getting "confused" between worksheets, and even if you use Alt+Tab to remove focus from Excel.
Note:
ExcelTips is your source for cost-effective Microsoft Excel training. This tip (2579) applies to Microsoft Excel 97, 2000, 2002, and 2003.
Create Custom Apps with VBA! Discover how to extend the capabilities of Office 2013 (Word, Excel, PowerPoint, Outlook, and Access) with VBA programming, using it for writing macros, automating Office applications, and creating custom applications. Check out Mastering VBA for Office 2013 today!
If you are using a macro to process a number of worksheets, you may have a need to know if the worksheet is empty or not. ...
Discover MoreNamed ranges are a great tool to use in developing formula-heavy workbooks. You may want, at some point, to copy your ...
Discover MoreThe graphics you place in a worksheet can do more than just look pretty. You can also assign macros to a graphic, which ...
Discover MoreFREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
There are currently no comments for this tip. (Be the first to leave your comment—just use the simple form above!)
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 © 2024 Sharon Parq Associates, Inc.
Comments