Excel like a finance pro.
VBA · Variables
ReDim
Resizes a dynamic array; Preserve keeps the existing values.
When to use it
ReDim sizes or resizes a dynamic array at runtime. With Preserve the existing values are kept, but only the last dimension can change.
The code
- Code
ReDim Preserve names(1 To n)
Worked examples
Size at runtime
Dim names() As String ReDim names(1 To n) → An array sized to n after n is known
Dynamic sizing.
Grow with Preserve
ReDim Preserve names(1 To n + 1) → One more slot, values kept
Growing a list.
Two dimensions
ReDim Preserve data(1 To 10, 1 To cols + 1) → Only the last dimension can grow with Preserve
The 2D limitation.
Worth knowing
- ReDim Preserve in a loop is slow; size once to the maximum, or use a Collection.
- ReDim without Preserve clears the array.
- Erase releases the memory.
Where it goes wrong
- Preserve cannot change the first dimension of a 2D array; transpose your layout.
- ReDim on a fixed-size array is a compile error.
Related
Learn the moves here — or let Wauvel run them on your numbers.
Meet your AI CFO →One CFO-grade Excel tip a week
A short, practical email for finance operators — functions, shortcuts, and the moves that save an afternoon. Free, unsubscribe anytime.