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

Automatically Converting to GMT

You know what time it is, right? (Quick—look at your watch.) What if you want to know what time it is in Greenwich, ...

Discover More

Plotting Times of Day

Got a chart created from your worksheet? You can plot times of day in the chart if you apply the simple techniques in ...

Discover More

Printing Summary Information from a Macro

Part of the information that Word maintains about each of your documents is a summary statement, which you can define in ...

Discover More

Comprehensive VBA Guide Visual Basic for Applications (VBA) is the language used for writing macros in all Office programs. This complete guide shows both professionals and novices how to master VBA in order to customize the entire Office suite for their needs. Check out Mastering VBA for Office 2010 today!

More ExcelTips (menu)

Easily Changing the Default Drive and Directory

Need a quick way to change the default drive and directory in a macro you are writing? Here's the commands to do it and a ...

Discover More

Inserting Worksheet Values with a Macro

Macros are often used to process information in a worksheet. You may need your macro to change the values stored in ...

Discover More

Displaying a Set Column Range

Do you want to display a particular range of columns within the Excel window? Here's a couple ways you can accomplish the ...

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 9 - 8?

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.