30 Advanced Excel Tips for 2026

Excel Champs / VBA / VBA Out of Memory Error (Error 7)
By Puneet Gogia (Microsoft MVP)

VBA Out of Memory Error (Error 7)

30+ Advanced Excel Tips for 2026

No spam. Only Excel Stuff. God Promise. 😊

Error 7, “Out of memory”, means Excel ran out of the memory it’s allowed to use while the macro was running. 32-bit Excel can use only about 2 GB (up to 4 GB on 64-bit Windows), however much RAM the PC has. Check File → Account → About Excel; if it says 32-bit, moving to 64-bit Office is the lasting fix for large workbooks.

In the code, the usual causes are very large arrays and copies. arr = Range("A:Z").Value loads about 27 million cells, so size the range to the data with Cells(Rows.Count, "A").End(xlUp).Row instead. A ReDim Preserve inside a loop that keeps growing an array uses up memory the same way.

Clear the clipboard after copying large ranges with Application.CutCopyMode = False, release object variables you’ve finished with, as in Set wb = Nothing, and close workbooks the macro opened. Closing other programs helps only when Windows itself is short of memory.

If the error appears while editing or compiling rather than running, a very large module is the likely cause, so split its procedures across several modules. Save your work and restart Excel before trying again, because memory used by a failed run isn’t always released.

VBA’s Error 7 (Out of Memory) occurs when Excel needs more memory to run your macro than is available. Every application you open in your system takes a part of the memory, and when you try to run a macro and the memory that’s left isn’t enough, you get error 7.

Apart from that, there are a few points that you need to check when you face this error (Out of Memory).

  1. As I said, you could have a lot of applications running, which makes the system run out of memory. You can close all the applications (and workbooks) that you don’t need and check if that works for you.
  2. You have a lot of lengthy codes in the same module, or one very long procedure. You can try to use different modules for those codes (if you have 10 different codes, try to split them into 3-4 modules) and break a long procedure into smaller ones. This doesn’t save memory, but it keeps a module or procedure under VBA’s 64K size limit.
  3. Windows runs out of virtual memory. This can’t be corrected until you free up disk space or increase the virtual memory (paging file) size.
  4. You have too many Public variables. The space for them is limited, so declare variables inside the procedures where you use them, and keep only the ones you really need as Public.
  5. The code tries to change a read-only property. Microsoft lists this as a cause of error 7 too, so check the line where the code stops and make sure the property can be changed.

Below I have shared a few links that can help you deal with this error.

Common Causes of Error 7 in Excel Macros

In today’s Excel, the error mostly comes from what the macro does with data, not from the system. Here are the things to look for in your code:

  • Huge arrays: A ReDim with millions of elements, or an array that keeps growing with ReDim Preserve inside a loop.
  • Whole columns read into an array: A line like arr = Range(“A:Z”).Value reads more than 27 million cells, even if your data has only a few hundred rows.
  • Objects that are never released: Creating objects (like workbooks, collections or dictionaries) in a loop without closing them or setting them to Nothing.
  • 32-bit Excel: 32-bit Excel can use only about 2 GB of memory (up to 4 GB on 64-bit Windows with the Large Address Aware update), however much RAM your computer has. 64-bit Excel doesn’t have this limit.

How to Fix Error 7 in Your Code

First of all, read only the cells that have data. In the code below, VBA finds the last row with data in column A and reads columns A to E up to that row, instead of reading whole columns:

Sub ReadUsedData()
Dim ws As Worksheet
Dim lastRow As Long
Dim arr As Variant
Set ws = ThisWorkbook.Worksheets("Sheet1")
lastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row
arr = ws.Range("A1:E" & lastRow).Value
MsgBox "Rows read: " & UBound(arr, 1)
End Sub

After that, release the memory you don’t need any more. Set object variables to Nothing and clear big arrays with Erase once you are done with them:

Set ws = Nothing
Erase arr

And if you resize an array in a loop, work out the size you need first and use ReDim once, before the loop, instead of ReDim Preserve on every pass. In the end, if your macro really needs a lot of memory, save your work, restart Excel, and if you can, switch to 64-bit Excel.

Frequently Asked Questions

How Do I Check If My Excel Is 32-Bit or 64-Bit?

Go to File ➤ Account ➤ About Excel. The first line of the window shows the version and says “32-bit” or “64-bit” at the end.

Does Adding More RAM Fix the Out of Memory Error?

Only with 64-bit Excel. 32-bit Excel can’t use more than its limit, however much RAM you have, so fix the code first (read only the data you need and release objects) or move to 64-bit Excel.

Why Do I Get Error 7 When the Code Ran Fine Before?

Your data may have grown, or Excel has been open for a long time with many workbooks. Close the workbooks you don’t need, restart Excel, and check whether the macro reads whole columns or keeps adding to an array.

Puneet Gogia

Written by

Puneet Gogia

Microsoft MVP — Excel · Founder, Excel Champs

Twelve years of teaching Excel, and a decade before that using it as a data analyst. Every tutorial here is written from a file I actually built.

3M+ readers helped
1,000+ tutorials written
Since 2015 teaching Excel