Arrays let you store and process many values in VBA without repeatedly reading and writing worksheet cells, which makes your macros dramatically faster and cleaner. The core skill is simple: read a range into an array in one line, loop through the array in memory, then write the result back to the sheet in one line. This guide walks through declaring, filling, resizing, reading and writing arrays in Excel VBA.
What You'll Need
- Excel (any version from Excel 2010 onward; the VBA language used here is identical across them)
- The Developer tab enabled, so you can open the Visual Basic Editor (or press Alt + F11 from any worksheet)
- A workbook saved as a macro-enabled file (.xlsm) if you want to keep the code
- Basic familiarity with writing and running a Sub procedure
Step-by-Step
- Open the VBA editor. Press Alt + F11, then insert a module via Insert > Module so you have somewhere to paste code.
- Declare a fixed-size array. Use
Dim nums(1 To 5) As Double. The numbers in parentheses are the lowest and highest index, so this array holds exactly five values. Declaring1 To 5instead of the default0 To 4avoids the classic off-by-one confusion. - Fill it in a loop. Example:
For i = 1 To 5: nums(i) = i * 10: Next i. Each element is assigned individually by its index. - Declare a dynamic array when the size is unknown. Write
Dim data() As Variantwith empty parentheses, then size it later withReDim data(1 To n). AddPreserve(ReDim Preserve data(1 To n + 1)) if you need to keep the existing values when resizing — note that Preserve can only change the last dimension. - Read a worksheet range into an array in one shot. This is the big speed win:
data = Range("A1:A100").Value. No loop, no ReDim — VBA sizes the Variant automatically. A multi-column range such asRange("A1:C100").Valueproduces a 2-D array indexed asdata(row, column), and the indexes are always 1-based when read from a range this way. - Find the array's bounds. Use
LBound(data)andUBound(data)instead of hard-coded numbers. For 2-D arrays pass a second argument:UBound(data, 2)gives the upper bound of the second (column) dimension. - Loop through the array. Nested loops for 2-D data:
For r = LBound(data, 1) To UBound(data, 1)For c = LBound(data, 2) To UBound(data, 2)' process data(r, c)Next cNext r - Write the results back in one shot.
Range("E1").Resize(UBound(data, 1), UBound(data, 2)).Value = datadumps the whole array at once. Make sure the target range is exactly the same shape as the array, or you will get extra empty cells or a truncated result. - Use Split and Join for text.
Split("a,b,c", ",")returns a 0-based String array;Join(arr, ", ")glues it back together. These are handy for building CSV-style output or parsing delimited text. - Erase when done with large arrays.
Erase datareleases the memory of a dynamic array (you must ReDim before using it again) and resets fixed-size arrays to empty values. Usually optional, but useful in long-running macros.
Common Mistakes
- Assuming all arrays are 1-based. Arrays read from a range are 1-based, but
Split,Arrayand most API results are 0-based. Always use LBound/UBound rather than guessing. - Dimension mismatch when writing back. Writing a 100-row array to a 50-row range fails or truncates. Use Resize with UBound as shown above.
- Forgetting that .Value on one cell is not an array.
Range("A1").Valuereturns a single Variant, not a 1-element array — UBound on it raises "Type mismatch". - Declaring the wrong type for range reads. A range containing text and numbers must be read into a Variant array; a
Double()array will throw an error if any cell holds text or an error value like #N/A. - Reading and writing cells inside a loop. Doing
Cells(i, 1).Valuethousands of times is far slower than one bulk read and one bulk write. That is the whole point of arrays.
Tips
- Turn off screen updating (
Application.ScreenUpdating = False) around bulk writes for extra speed, and set it back to True at the end. - Test new array logic with
Debug.Printand the Immediate window (Ctrl + G in the editor) instead of writing to cells just to inspect values. - For a 1-D array from a single column, you can still index it with one subscript only if it truly is 1-D; a range read never is — use
data(r, 1). Application.Index(array, 0, 2)pulls a single column out of a 2-D array when you need it.- For very large data sets, working with arrays in memory and writing once is often orders of magnitude faster than cell-by-cell code.
Troubleshooting
- "Subscript out of range" (run-time error 9): an index is outside LBound–UBound. Print the bounds in the Immediate window and check whether the array is 0- or 1-based.
- "Type mismatch" (error 13): usually a Variant array read from a range containing errors or text being forced into a typed array, or calling UBound on a non-array. Read into Variant and inspect the source cells.
- "Can't assign to array": you tried to assign to the array variable itself after a fixed-size declaration, or assigned a non-array. Only dynamic Variants can be filled directly from a range.
- Values come out wrong when written back: the target range shape doesn't match the array, or a 2-D array was written into a 1-row range. Rebuild the target with Resize using UBound for both dimensions.
- ReDim Preserve fails on the first dimension: that is by design — Preserve can only resize the last dimension. Transpose your layout (columns as the last dimension) or collect into a Collection or Dictionary first, then convert.
Frequently Asked Questions
Are arrays really faster than reading cells directly?
Yes, substantially. Each cell read or write is a call across the object model, which is slow; reading a whole range into an array once and writing it back once can reduce run time from minutes to under a second for large data sets.
What's the difference between Dim arr(5) and Dim arr() As Variant?
Dim arr(5) creates a fixed array of six elements (indexes 0–5) that can never be resized. Dim arr() As Variant declares a dynamic array you size with ReDim at runtime — the only kind you can load directly from a range.
Do arrays read from a range start at index 0 or 1?
They start at 1 for both rows and columns, regardless of any Option Base setting. However, Split and Array results are 0-based, so always check with LBound when in doubt.
Can a VBA array hold more than two dimensions?
Yes — you can declare arrays with up to 60 dimensions, though anything beyond three is rarely practical and gets hard to visualize. Most Excel work only needs one or two.
How do I sort an array in VBA?
VBA has no built-in array sort. Common approaches: write the array to a temporary range and use Range.Sort, use an ArrayList (via CreateObject("System.Collections.ArrayList")) with its Sort method, or implement a simple bubble or quicksort in VBA.
Does Option Base 1 affect arrays from ranges?
No. Option Base 1 only changes the default lower bound of arrays you declare with Dim without explicit bounds. Arrays assigned from a range are always 1-based; arrays from Split and Array are always 0-based.