How to clear the entire array?

calendar_today Asked Jun 10, 2010
thumb_up 58 upvotes
history Updated April 14, 2026

Direct Answer

You can either use the Erase or ReDim statements to clear the array: Dim threeDimArray(9, 9, 9), twoDimArray(9, 9) As Integer Erase threeDimArray, twoDimArray ReDim…. This is a 4-line Excel VBA snippet, ranked #6th of 303 by community upvote score, from 2010.


The Problem (Q-score 29, ranked #6th of 303 in the Excel VBA archive)

The scenario as originally posted in 2010

I have an array like this:

Dim aFirstArray() As Variant

How do I clear the entire array?
What about a collection?

Why community consensus is tight on this one

Across 303 Excel VBA entries in the archive, the accepted answer here holds elite answer (top 10 %%) status — meaning voters are unusually aligned on the right fix.


The Verified Solution — elite answer (top 10 %%) (+58)

4-line Excel VBA pattern (copy-ready)

You can either use the Erase or ReDim statements to clear the array:

Dim threeDimArray(9, 9, 9), twoDimArray(9, 9) As Integer
Erase threeDimArray, twoDimArray
ReDim threeDimArray(4, 4, 9)

See the different usage of each method here.

Update

To remove a collection, you iterate over its items and use the remove method:

For i = 1 to MyCollection.Count
  MyCollection.Remove 1 ' Remove first item
Next i

Loop-performance notes specific to this pattern

The loop in the answer iterates in process. On a 2026 Office build, setting Application.ScreenUpdating = False and Application.Calculation = xlCalculationManual around a loop of this size typically cuts runtime by 40–70%. Re-enable both in the Exit handler.


When to Use It — vintage (14+ years old, pre-2013)

A top-10 Excel VBA pattern — why it still holds up

Ranks #6th of 303 in the Excel VBA archive. The only pattern ranked immediately above it is “IF statement: how to leave cell blank if condition is false (""…” — compare both if you’re choosing between approaches.

What changed between 2010 and 2026

The answer is 16 years old. The Excel VBA object model has been stable across Office 2013, 2016, 2019, 2021, 365, and 2024/2026 LTSC, so the pattern still compiles. Changes that might affect you: 64-bit API declarations (use PtrSafe), blocked macros in downloaded files (Mark-of-the-Web), and the shift toward Office Scripts for web-first workflows.

help
Frequently Asked Questions

Why is this answer the top decile of Excel VBA Q&A?
expand_more

Answer score +58 vs the Excel VBA archive median ~19; this entry is elite. The score plus 29 supporting upvotes on the question itself (+29) means the asker and 57 subsequent voters all validated the approach.

Does the 4-line snippet run as-is in Office 2026?
expand_more

Yes. The 4-line pattern compiles on Office 365, Office 2024, and Office LTSC 2026. Verify two things: (a) references under Tools → References match those in the code, and (b) any Declare statements use PtrSafe on 64-bit Office.

This answer is 16 years old. Is it still relevant in 2026?
expand_more

Published 2010, which is 16 year(s) before today’s Office 2026 build. The Excel VBA object model has had no breaking changes in that window. Three things to re-test: (1) blocked macros on downloaded files (Mark-of-the-Web), (2) 64-bit API declarations (PtrSafe, LongPtr), (3) any shift toward Office Scripts for web scenarios.

Which Excel VBA pattern ranks just above this one at #5?
expand_more

The pattern one rank above is “IF statement: how to leave cell blank if condition is false ("" does not work)”. If your use case overlaps, compare both before committing.

Data source: Community-verified Q&A snapshot. Q-score 29, Answer-score 58, original post 2010, ranked #6th of 303 in the Excel VBA archive. Last regenerated April 14, 2026.