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.
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!
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
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
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
FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
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.