Out of Memory Errors for Macros

by Allen Wyatt
(last updated February 9, 2019)

Jeff has a sophisticated Excel workbook that uses VBA to generate numerous reports in PDF format. It runs great on Excel 2013 32-bit across multiple PCs. Conversely, on multiple other PCs running Office 365, halfway through the output Jeff receives an "Out of Memory" error. Thus, the macros work perfectly on the older version of Excel, but not on the newest version. (Again, Jeff tested this across multiple PCs so the results are repeatable.) Jeff wonders is there are known memory leaks or macro differences in Office 365 that could be causing this problem.

Generally, memory usage has to do with one primary issue—variables and how they are used in your macro. It specifically comes into play with variable arrays. It is always a good idea to make sure you declare all your variables (use the Option Explicit directive to help in this regard) and check your array dimensioning to make sure you are not trying to declare an insanely huge array.

Object variables can also use a lot of memory. If you use the Set keyword to assign an object to a variable, make sure you set the object variable to Nothing when you are done. This is especially important inside of loops—if each pass through the loop results in the declaration of another object variable, if you fail to clear that variable during each iteration of the loop, you can use up memory very quickly.

There is also a point to be made for turning off screen updating while your macro is going through its gyrations. If your macro tries to continually update the screen, that uses resources and slows down your macro.

One would think that in Jeff's situation, though, memory usage wouldn't be a huge issue. After all, he is moving from a 32-bit version of Excel to (more than likely) a 64-bit version. This allows Excel to utilize a larger memory space than ever before. Thus, one would think that "Out of Memory" errors would be less likely rather than more likely.

Unfortunately, there is a wrench to throw into the mix here. When you get an "Out of Memory" error, it may not be memory that is the culprit. Microsoft has documented, over the years, that this particular memory is generic in nature and it can be caused by just about anything. (In my book, that makes the error message useless, but who am I to question Microsoft. Frustrating!)

The other thing I would consider doing (not having seen Jeff's code) is to make sure you are not programming linearly. In other words, break your code up into individual subroutines that preform small, discrete tasks. You can then call each subroutine from a main, controlling routine. The benefit to this is memory management due to scope. Variables have scope only within the procedure in which they are used. (Well, this is true unless you declare them as having global scope, but that is a different kettle of fish.) This means that variables used within a procedure are destroyed and their memory freed up, automatically, when the procedure is exited.

If your macro, however, is linearly written so that there are no subroutines, then all variables are maintained in your system for the entire time the macro is running. It is better, from a memory management standpoint, to break up your code into procedures so you can minimize memory usage.

The other advantage to modularizing your code in this way is that if you do encounter an error—even the generally worthless "Out of Memory" error—it will likely occur within a specific procedure and you can thereby have a better chance at pinpointing where the actual error is occurring. To my thinking, this ability to more efficiently track down the potential point of failure is a big benefit.

If this still doesn't clear up the problem, I have seen some reports that when you save your workbook it forces VBA to do garbage collection on your memory space. Because of this, some people suggest having your macro save the workbook every so often, rather than just at the end of the macro. I don't know how accurate this reporting is, but if you are already using your macro to save the workbook, it can't hurt to have it save more often rather than less.

There is one final thing that should be mentioned. If you're now using Office 365 Business Premium, it includes SharePoint Online and Excel Web App. These versions, therefore, limit the size of an Excel file to 10 MB. If your file is quite large, it may have worked just fine under Excel 2013, but fail under Office 365 Business Premium because of this file size limitation.

ExcelTips is your source for cost-effective Microsoft Excel training. This tip (13619) applies to Microsoft Excel 2007, 2010, 2013, 2016, 2019, and Excel in Office 365.

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

Using the GotoButton Field

Need to jump from one place in your document to another? One way to do this is through the user of the GotoButton field, ...

Discover More

Previewing Before You Print

Print Preview is a feature built into Word that allows you to see what your printed output will look like before you ...

Discover More

Understanding Page Border Art

Add some artwork around the border of your printed page, and you may not know where that artwork comes from. You may also ...

Discover More

Professional Development Guidance! Four world-class developers offer start-to-finish guidance for building powerful, robust, and secure applications with Excel. The authors show how to consistently make the right design decisions and make the most of Excel's powerful features. Check out Professional Excel Development today!

More ExcelTips (ribbon)

Changing Directories in a Macro

Need to specify which directory on your hard drive should be used by a macro? It's easy to do using the ChDir command.

Discover More

Stopping a Checked Box from being Unchecked

When creating user forms for use in Excel, you are provided with a range of controls you can add, including check boxes. ...

Discover More

Separating Evens and Odds

If you have a series of values in a column, you might have a need to separate the values into even values and odd values. ...

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}] 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 five more than 3?

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 ribbon interface (Excel 2007 or later)? This site is for you! If you use an earlier version of Excel, visit our ExcelTips site focusing on the menu 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.