Run-time error '7': Out of memory

梦想与她 提交于 2019-12-04 12:11:48

This has happened to me before. I had the same solution, exit excel, free up some memory and try again - and it worked. You may have to shut down other programs while using this. Its literally what it says it is, lack of available memory.

Keep in mind that if you've run other macros that copy information to the clipboard, you will have less RAM freed up to run the macro.

Also, are you using 32 or 64 bit Excel - 64 will allow you to use more RAM.

I notice that you not set oRange to nothing when cleaning up your sub, could it be that this object is using a lot of memory which isn't being released when the sub ends?

I had a similar error and finally traced it down to the "For Each" statement. I think it has to do with the memory allocation for the Collection, Doc.ActiveDocument.InlineShapes in your example.

My bad code (PowerPoint to Excel):

For Each sh In InputBook.Sheets("Exec Sum").Shapes
    sh.Visible = False
Next
Set sh = Nothing

My fixed code:

For i = 1 To InputBook.Sheets("Exec Sum").Shapes.Count
    InputBook.Sheets("Exec Sum").Shapes(i).Visible = False
Next

Avoiding a reference to a collection solved my issue.

The frequent access to the worksheet can create problems with resource usage. The way to go about this is to fetch data in a single access point, like

Dim V as Variant
V = InputRange
' Now V becomes a m x n array of the cell values in InputRange
' you may manipulate and work with this data and fill all your results in 
' OutputV(m,n) variant array

Dim OutputV() as Variant
ReDim OutputV(m,n)
oRange = OutputV

Usually speeds up the code by several hundred times depending on the size of the range and also uses far less resources.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!