Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

Excel VBA Resize Array: Step-by-Step Guide

· 6 min read · Home Tech

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

You cannot resize a fixed-size array in VBA — once you declare Dim arr(1 To 5), that array keeps its bounds for its whole lifetime. The working answer is to use a dynamic array declared with empty parentheses (Dim arr() As Variant) and then set its size with ReDim. ReDim arr(1 To n) allocates the new size, and ReDim Preserve arr(1 To n) keeps the existing values while changing the size. The catch is that Preserve can only change the last dimension, and it is slow if you call it inside a large loop. The short recipe: declare dynamic, ReDim to size, fill it, then ReDim Preserve to grow or shrink — or size it once from a count and avoid resizing altogether.

What You'll Need

  • Microsoft Excel on Windows or Mac with the Visual Basic Editor available (Alt+F11 on Windows, or Developer tab > Visual Basic).
  • A macro-enabled workbook (.xlsm) if you want to keep the code.
  • Basic familiarity with declaring variables and running a macro.

Menu and ribbon labels differ slightly between Excel versions and between Windows and Mac, so if a label does not match exactly, look for the closest equivalent.

Step-by-Step

  1. Open the editor. Press Alt+F11 (Windows) or use Developer > Visual Basic. If the Developer tab is hidden, enable it via File > Options > Customize Ribbon and tick Developer.
  2. Insert a module. In the editor, choose Insert > Module. This is where your procedure lives.
  3. Declare a dynamic array. Type Dim arr() As Variant. The empty parentheses are what make it dynamic. A declaration like Dim arr(1 To 10) is fixed and cannot be resized at all.
  4. Allocate the first size with ReDim. Write ReDim arr(1 To 5). This creates five slots, indexed 1 through 5. You can use any valid lower and upper bound, including 0-based bounds.
  5. Fill the array. Assign values in a loop, for example For i = 1 To 5: arr(i) = i * 10: Next i. At this point the array holds five values.
  6. Grow it while keeping values. Write ReDim Preserve arr(1 To 10). The first five values survive; slots 6 to 10 are empty (Empty for a Variant array). Without Preserve, every existing value is wiped.
  7. Shrink it the same way. ReDim Preserve arr(1 To 3) keeps the first three values and discards the rest. Shrinking with Preserve is allowed as long as the new upper bound is not below the lower bound.
  8. Read the current bounds before you trust them. Use LBound(arr) and UBound(arr) rather than hard-coding numbers. After the step above, UBound(arr) returns 3.
  9. Prefer sizing once from a count. If you know how many items you need, count them first (for example with WorksheetFunction.CountA on a range) and ReDim a single time. This is far faster than resizing repeatedly.
  10. Run the macro. Press F5 with the cursor inside the procedure, or assign it to a button via Developer > Insert > Button (Form Control). Use Debug > Step Into (F8) to walk through line by line and watch the array in the Locals window.

Common Mistakes

  • Declaring a fixed array and trying to resize it. Dim arr(1 To 5) followed by ReDim arr(1 To 10) raises a compile error. Only arrays declared with empty parentheses can be re-dimensioned.
  • Forgetting Preserve. A plain ReDim on an existing array clears all values. If your data disappears after a resize, this is almost always why.
  • Using Preserve on a multi-dimensional array's first dimension. ReDim Preserve only works on the last dimension. For a 2D array you can resize the column count but not the row count. The usual workaround is to transpose the data or restructure so the changing dimension is last.
  • Assuming indexes start at 0. Unless you write Option Base 1 or declare explicit bounds, ReDim arr(10) gives indexes 0 to 10 — eleven slots, not ten. Always check with LBound.
  • Resizing inside a big loop. Each ReDim Preserve copies the whole array. Doing it once per item over thousands of rows turns a fast macro into a slow one.
  • Mixing types. A Variant array accepts anything; a typed dynamic array such as Dim arr() As Long only accepts Long values. Assigning text to it raises a type mismatch.

Tips

  • For unknown-size output, collect into a Collection or ArrayList-style approach, then size the array once at the end. It avoids repeated copying entirely.
  • Batch your growth: when you must resize in a loop, double the size each time it fills up and track a separate counter for used slots. This keeps the number of copies small.
  • Use Erase arr to free the memory of a dynamic array when you are done, especially in long-running macros.
  • Write helper routines like AppendToArray once and reuse them instead of repeating the Preserve logic in every procedure.
  • Keep the array's lower bound consistent across your code. Mixing 0-based and 1-based arrays in the same procedure is a common source of off-by-one bugs.

Troubleshooting

"Compile error: Expected array" or subscript out of range. Check that the array was declared with empty parentheses and that you are reading within LBound to UBound. After a shrink, old indexes no longer exist.

Values vanish after resizing. You used ReDim without Preserve. Add it — but remember it only preserves the last dimension.

"Subscript out of range" on a 2D array with Preserve. You tried to change the first dimension. Restructure the data so the growing dimension is the second one, or rebuild the array into a new one.

Macro runs slowly. Look for ReDim Preserve inside a loop. Move it outside, pre-count the items, or grow in chunks. Also switch off ScreenUpdating while the macro runs and turn it back on at the end.

Type mismatch. A typed array is rejecting a value of the wrong type. Either declare the array as Variant or convert the value before assigning it.

If that did not work, simplify: write a five-line test procedure that declares a dynamic array, ReDims it, fills it, and prints UBound to the Immediate window with Debug.Print. Confirm that works before adding your real logic back.

Frequently Asked Questions

Can I resize an array declared as Dim arr(1 To 5)?

No. That is a fixed-size array and VBA will not let you change its bounds. Redeclare it with empty parentheses and use ReDim.

Does ReDim Preserve keep all my values?

It keeps the values in the slots that still exist after the resize. If you shrink the array, values beyond the new upper bound are lost permanently.

Can I resize a two-dimensional array?

Only the last dimension. ReDim Preserve arr(1 To 5, 1 To 10) can become arr(1 To 5, 1 To 20), but you cannot change the 5. For row growth, transpose the data or rebuild the array.

What is the difference between ReDim and ReDim Preserve?

ReDim allocates a new size and clears the contents. ReDim Preserve allocates a new size and copies the surviving values across.

Why does UBound return a different number than I expected?

Arrays are 0-based by default, so ReDim arr(10) gives indexes 0 to 10. Use LBound and UBound instead of assuming, or declare explicit bounds like 1 To 10.

Is there a faster alternative to resizing in a loop?

Yes. Count the items first and size the array once, or collect values in a Collection and transfer them to an array at the end. Both avoid repeated copying.