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

Spelling Errors on Internet Addresses

Tired of Word marking Internet addresses as spelling errors? You can turn off this check by applying the steps in this tip.

Discover More

Fixing Macro Button Behavior in Protected Worksheets

When working with macro buttons, you may run into some bizarre behavior related to the macros without really ...

Discover More

Embedding an Excel Chart in a Word Document

Word and Excel usually work pretty well together. This means that you can easily paste charts from Excel into your Word ...

Discover More

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!

More ExcelTips (menu)

Displaying Messages When Automatic Data Changes

It is possible to develop macros that update the information in your worksheets automatically. In such instances, you may ...

Discover More

Controlling the Behavior of the Mouse Wheel

The mouse wheel, by default, controls scrolling vertically through your worksheet. If you don't want the wheel to control ...

Discover More

Documenting Changes in VBA Code

Your company may be regulated by requirements that it document any changes to the macros in an Excel worksheet. Your ...

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 1 + 1?

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.