Out of Memory Errors for Macros

Written by Allen Wyatt (last updated October 16, 2023)
This tip applies to Excel 2007, 2010, 2013, 2016, 2019, and Excel in Microsoft 365


1

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 Microsoft 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

Special Differences when Searching

Word includes two different search engines. Which search engine you choose to use will dictate what Word shows as ...

Discover More

Saving in a Macro Using a Desired File Name

Need to save a new document, from within a macro, to a specific file name? If you use the Record Macro capabilities of ...

Discover More

Typing a Schwa Character in Excel

How you add special characters to Excel can differ from how you add them in other Office programs, such as Word. This tip ...

Discover More

Solve Real Business Problems Master business modeling and analysis techniques with Excel and transform data into bottom-line results. This hands-on, scenario-focused guide shows you how to use the latest Excel tools to integrate data from multiple tables. Check out Microsoft Excel 2013 Data Analysis and Business Modeling today!

More ExcelTips (ribbon)

Storing Macros in Templates

How Excel uses templates is different than how Word uses templates. This tip looks at those differences and discusses ...

Discover More

Removing a Directory

Macros allow you to perform all sorts of file-related operations. One such operation allows you to delete a directory. ...

Discover More

Unprotecting Groups of Worksheets

Unprotecting a single worksheet is relatively easy. Unprotecting a whole lot of worksheets is harder. Here's how you can ...

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 6 + 5?

2023-05-10 21:20:03

Tomek

I am not sure this may play any role in Jeff's problems, but the fact that the new computer he is using is a 64 -bit machine does not necessarily mean that the Office version he has on his computer is also 64-bit. At my work we had to install 32 bit version of Office Pro because several applications were programmed using 32-bit libraries and would not work with 64-bit office. It was particularly Access databases that used specific libraries and also interacted with other applications, including generating e-mails and reports.

Anyway, what I am suggesting is to check for the 32 vs. 64 bit versions of Office, and may be switch to the other than what is currently installed to do some testing. Remember though that 32-bit versions have lower limit for the file sizes.

As a side note, I have seen Out-of-Memory errors when running macros or some other operations in workbooks, and I think they were related to file corruption. I was usually able to recover from this by creating a new file and copying and pasting the data from old workbook and moving macros between the files (lots of work). This wasn't specific to Excel, I have seen this in Word too.


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.