Excel VBA arrays: declare, fill and write back fast
An array is one variable with several numbered slots. It lets you hold a whole list in memory instead of touching each cell on its own. Your macros get easier to read and, on large sheets, many times faster.
In short: Dim sales(1 To 5) As Double creates five slots, and sales(3) = 120 fills the third. UBound(sales) returns the highest index. data = Range("A2:D100").Value reads a whole range into a two-dimensional array, and Range("A2:D100").Value = data writes it back in one go.
What you need to know: loops from For Next and faster macros from With and ScreenUpdating.
Try it right now
The daily sales are listed newest first. You read them into an array and write them out the other way round. The array is the scratch pad you read from back to front.
Task A2:A6 holds the daily sales of the last five days, newest at the top. The macro ReverseList should first read them into the array sales(1 To 5) and then write them to B2:B6 in reverse order. When writing back, loop backwards through the array using UBound and LBound.
| A | B | C | D | |
|---|---|---|---|---|
| 1 | Newest first | Oldest first | ||
| 2 | 1840 | |||
| 3 | 2210 | |||
| 4 | 1975 | |||
| 5 | 2430 | |||
| 6 | 2105 |
Declaring an array
Dim sales(1 To 5) As Double ' fixed size, slots 1 to 5
Dim names() As String ' dynamic, no size yet
Dim data As Variant ' will hold a whole range later
sales(1) = 1840
ReDim names(1 To 3) ' now three slots
Debug.Print UBound(sales) ' 5
| Syntax | What it does |
|---|---|
Dim a(1 To 5) | Fixed size with indexes 1 to 5. The bounds cannot change later. |
Dim a(5) | Without a lower bound VBA starts at 0: that is six slots, 0 to 5. Spelling out the lower bound avoids surprises. |
Dim a() and ReDim a(1 To n) | Dynamic array: you set the size once you know it, even from a variable. |
ReDim Preserve a(1 To n + 1) | Grows the array and keeps its contents. Without Preserve everything is wiped. |
LBound(a), UBound(a) | Lowest and highest index. With two dimensions, the second argument picks the dimension: UBound(a, 2) is the number of columns. |
Write loops over an array with these two functions, For i = LBound(sales) To UBound(sales), and they fit no matter how big the array is.
Reading and writing a range in one step
This is the real reason arrays are so popular in Excel macros. Every trip to a cell costs time, a trip to an array in memory costs almost nothing. With a few rows you will not notice; with tens of thousands you will.
Dim data As Variant, i As Long
data = Range("A2:D500").Value ' read once
For i = 1 To UBound(data, 1)
data(i, 4) = data(i, 2) * data(i, 3)
Next i
Range("A2:D500").Value = data ' write once
A range of several cells always becomes a two-dimensional array: data(row, column). Both indexes start at 1 and count within the range, so data(1, 1) is A2 here, not A1. A single cell is different: Range("A2").Value is just the value, not an array.
Task The macro LineTotals should put quantity times price into column D for every row, but fast: read A2 to D of the last row into the variable data with one single assignment, calculate inside the array and write the whole array back in one step. The number of rows changes; Check tests with more rows.
| A | B | C | D | |
|---|---|---|---|---|
| 1 | Item | Qty | Price | Total |
| 2 | Copy paper A4 | 20 | 4.85 | |
| 3 | Black toner | 3 | 64.90 | |
| 4 | Lever arch file | 25 | 2.40 | |
| 5 | Blue ballpoint pens | 50 | 0.65 | |
| 6 | Sticky notes | 12 | 1.95 |
This exercise is part of Premium.
Premium unlocks every exercise in every lesson, unlimited practice with fresh tasks and the final test with certificate.
See Premium Already Premium? Log in
The first exercise in this lesson is free.
Growing with ReDim Preserve
Often you do not know in advance how many entries there will be, every customer with an open invoice, for example. Then the array grows as you go: for each match, count up and enlarge it with ReDim Preserve. At the end, Join from string functions glues everything into one piece of text.
Task The macro Reminders should collect every customer with an open balance above 0 in a dynamic array and then write them to E2 with Join, separated by a comma and a space. You do not know in advance how many there are: grow the array with ReDim Preserve each time a customer is added.
| A | B | C | D | E | |
|---|---|---|---|---|---|
| 1 | Customer | Invoice | Open | Reminder list | |
| 2 | Carter Bakery | R-1043 | 0.00 | ||
| 3 | Peters Motors | R-1044 | 1,250.00 | ||
| 4 | Dr. Patel's Practice | R-1045 | 0.00 | ||
| 5 | Greenway Garden Centre | R-1046 | 380.50 | ||
| 6 | Brandt Electrical | R-1047 | 0.00 |
This exercise is part of Premium.
Premium unlocks every exercise in every lesson, unlimited practice with fresh tasks and the final test with certificate.
See Premium Already Premium? Log in
The first exercise in this lesson is free.
ReDim Preserve copies the whole array on every call. For a few hundred entries that does not matter. For very long lists, size the array generously once and trim it at the end.
In real Excel
In the VBA editor you can look inside an array in the Locals window (View > Locals Window). Set a breakpoint after the line that fills it and expand the variable with the plus sign: every slot is listed with its index and value. Debugging shows how that works step by step.
Common mistakes
Subscript out of range
If a loop goes one step too far, say For i = 1 To 6 with Dim sales(1 To 5), VBA reports "Run-time error '9': Subscript out of range". You get the same error when you start at 0 on an array that came from a range: data(0, 1) does not exist. The exercise hint tells you the index and the valid bounds.
ReDim without Preserve
Writing ReDim names(1 To count) without Preserve in the reminder list gives no error, just a wrong result: E2 shows ", Greenway Garden Centre". Each ReDim wiped the names collected so far, only the last one survives.
Row and column swapped
data(i, 4) is row i, column 4. Writing data(4, i) reads across the array and soon hits error 9, because there are not that many columns. The order is the same as in Cells(row, column).
Range and array of different sizes
If you write an array with five rows into Range("D2:D10"), the extra cells get the error value #N/A. Use the same address for reading and writing, or size the target with Resize(UBound(data, 1), UBound(data, 2)).
FAQ
Why is data declared As Variant and not As Double?
Range(...).Value returns a Variant that holds an array. Cells can mix numbers, text and dates, and only a Variant takes all of them. That is why you declare the variable without parentheses, as a Variant.
Can I resize a fixed array?
No. Dim a(1 To 5) stays at five slots; ReDim on it is a compile error. If the size has to change, declare with empty parentheses and set the size with ReDim.
Does ReDim Preserve work with two dimensions?
Yes, but only for the last dimension. With data(row, column) you can add columns, not rows. For a growing list with several values per entry, a Dictionary is often easier.
Is Range.Value really faster than a loop over cells?
For small ranges the difference is invisible. It grows with every cell, because each Cells(i, j) call goes through Excel's object model, while an array lookup stays inside VBA. With tens of thousands of cells the one-step version is the usual choice.