Written by Allen Wyatt (last updated October 16, 2023)
This tip applies to Excel 2007, 2010, 2013, 2016, 2019, and Excel in Microsoft 365
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.
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!
Macros are great at working with text. This tip presents an example that shows this versatility by reversing the contents ...
Discover MoreYou can use macros to make your common Excel tasks easier and faster. For instance, if you routinely need to create new ...
Discover MoreYou can, from within your macros, easily display a message box containing a message of your choice. If you want to ...
Discover MoreFREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
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.
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.
FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
Copyright © 2024 Sharon Parq Associates, Inc.
Comments