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

Talking to Yourself

Need to keep notes about a document, but you don't want others to see those notes either on-screen or on-paper? Here's an ...

Discover More

Printing Portions of Mail Merged Documents

When you use a data source to create a bunch of documents in a mail merge, you might not want to print all the documents ...

Discover More

Ribbon Acting Strangely

The ribbon is the place where Word stores all the tools you need to work with your documents. What happens when the ...

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 (ribbon)

Clearing the Undo Stack in a Macro

Excel keeps track of the actions you take so that you can undo those actions if any are taken in error. You may want to ...

Discover More

Running Macros in the Background

Want to run a macro in Excel, but not sure if doing so will tie up your computer? Here's how macro processing really happens.

Discover More

Finding Positions of Formatted Characters in a Cell

With a little bit of work, Excel allows you to format individual characters of the text you place in a cell. If you want ...

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 eight 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.