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

Creating a Spelling Exclusion List

Don't like it when Word always says a word is spelled right and you know that it isn't? Here's how you can fine-tune the ...

Discover More

Removing All Bookmarks

Need to get rid of a lot of bookmarks all at once? Word doesn't provide a way to do it, but you can use the short macro ...

Discover More

Selecting an Entire Section

Documents can be subdivided into sections, with each of them formatted differently. If you want to select all the text in ...

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)

Testing for an Empty Worksheet

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 More

Copying Named Ranges

Named ranges are a great tool to use in developing formula-heavy workbooks. You may want, at some point, to copy your ...

Discover More

Assigning Macros to Graphics

The graphics you place in a worksheet can do more than just look pretty. You can also assign macros to a graphic, which ...

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 more than 7?

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.