HOWTO · VBA
How to Declare and Initialize String Array in VBA
This tutorial demonstrates how to declare and initialize string array in VBA.
On this page
In VBA, use a dynamic String() array when its size is determined as the program runs, or declare explicit bounds when the number of elements is known. You can populate a dynamic array with Split() or store the result of Array() in a Variant; the latter is a Variant containing an array, not a typed string array. The examples below show how to initialize each form, choose bounds, and work with two dimensions.
Declare and initialize a dynamic string array
A Variant can hold the array returned by Array(). Its elements are Variants, so this is useful when you want the convenience of a literal list and do not need a String() declaration. An unqualified Array() call uses the module’s Option Base setting for its lower bound. With the default Option Base 0, the fourth item is at index 3:
Dim animals As Variant
animals = Array("Lion", "Tiger", "Cheetah", "Monkey", "Elephant", "Zebra")
Debug.Print animals(3) prints Monkey.
For a typed dynamic string array, declare the variable with empty parentheses and assign the result of Split(). Split() separates a delimited string into a zero-based array of strings, regardless of the module’s Option Base setting. This is useful when the values come from a single delimited input:
Dim animals() As String
animals = Split("Lion,Tiger,Cheetah,Monkey,Elephant,Zebra", ",")
Debug.Print animals(2) prints Cheetah.
Use ReDim when you need to allocate or resize a dynamic array before assigning elements individually. The declaration Dim animals() As String declares the typed array variable; ReDim animals(0 To 5) allocates six string elements with indexes 0 through 5.
Declare and initialize a fixed-size string array
When the number of elements is known, declare the lower and upper bounds directly. Dim animals(0 To 5) As String creates six elements because both endpoints are included. Assign each value using its index, then read the desired element:
animals(0) = "Lion"
animals(1) = "Tiger"
animals(2) = "Cheetah"
animals(3) = "Monkey"
animals(4) = "Elephant"
animals(5) = "Zebra"
Debug.Print animals(4) prints Elephant.
Explicit bounds make the valid indexes clear and avoid relying on a module default. In an actual procedure, place the declaration and assignments together, for example inside Sub StaticArrayDemo() and before its End Sub.
Choose array bounds and use Option Base
Option Base sets the default lower bound for arrays declared in that module without an explicit lower bound. Put Option Base 1 in the module’s declarations section, before procedures. Then Dim animals(6) As String has indexes 1 through 6 in that module. This directive does not change arrays whose bounds are written explicitly, so Dim animals(0 To 5) As String still uses indexes 0 through 5.
For clarity, explicit bounds are often easier to read when adapting code. An unqualified Array() call follows the module’s Option Base: it has a lower bound of 0 with Option Base 0 and 1 with Option Base 1. By contrast, Split() always returns a zero-based array, regardless of Option Base. Use VBA.Array() when you need an Array() result whose lower bound is unaffected by the module setting; that qualified call uses a lower bound of 0. Keep these differences in mind when combining arrays or choosing indexes.
Declare and populate a multidimensional string array
Declare one lower and upper bound for each dimension. In Dim values(1 To 5, 1 To 5) As String, the first index selects a row and the second selects a column, giving 25 string elements. Nested loops can fill each position; declaring both counters as Integer ensures each variable has the intended numeric type:
Dim values(1 To 5, 1 To 5) As String
Dim i As Integer, j As Integer
For i = 1 To 5
For j = 1 To 5
values(i, j) = "The value of (" & i & "," & j & ") is " & i * j
Debug.Print values(i, j)
Next j
Next i
This fills the grid with a label and the product of its row and column indexes; for example, position (3, 4) contains The value of (3,4) is 12. Keep the dimensions and loop limits aligned so every assignment addresses an index declared in the array.
Run this small Python example to see the same zero-based selection pattern used above:
animals = ["Lion", "Tiger", "Cheetah", "Monkey", "Elephant", "Zebra"]
print(animals[3])
Monkey