Getting Big Macros to Run

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:

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 (2579) applies to Microsoft Excel 97, 2000, 2002, and 2003.

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

Breaking a Document Link

Word allows you to link external information into your documents. If you no longer need to maintain the active link, you ...

Discover More

Using MPF Graphic Files

There are all sorts of file formats used to store graphics. You might think that one of those formats is the MPF format, ...

Discover More

Running a Macro when a Worksheet is Deactivated

When you change from one worksheet to another, you may want to have Excel automatically run a macro for the worksheet you ...

Discover More

Save Time and Supercharge Excel! Automate virtually any routine task and save yourself hours, days, maybe even weeks. Then, learn how to make Excel do things you thought were simply impossible! Mastering advanced Excel macros has never been easier. Check out Excel 2010 VBA and Macros today!

More ExcelTips (menu)

Making Common Functions Available to Others

When you use macros to create functions, you might want to share those functions with others, particularly if they ...

Discover More

Moving Macros from the Personal Workbook

Need to move a macro out of your Personal.xls workbook and into a regular workbook? You can do it using familiar editing ...

Discover More

Creating a Photo Catalog from a Folder of Photos

Excel is great for collecting all sorts of information. You might even use it to create a catalog of your photos. Working ...

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 less than 9?

There are currently no comments for this tip. (Be the first to leave your comment—just use the simple form above!)


This Site

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.

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.