Learn about VBA Arrays
Introduction
Sometimes, developers are in a position to hold more than one value in a single variable at a time. When a series of values are stored in a single variable, then it is known as an array variable.
In VBA, an array is a data structure that allows you to store multiple values in a single variable, where each value is identified by an index or subscript. Arrays are useful when you need to work with lists or groups of related values, such as numbers, strings, or other data types.
Feature | Description | Example |
Static Array | Fixed size, defined at declaration. | Dim arr(4) As Integer |
Dynamic Array | Size can change during runtime using ReDim | Dim arr() As Integer : ReDim arr(1 To 10) |
Multidimensional Array | Array with multiple dimensions (e.g., 2D, 3D) | Dim arr(1 to 3, 1 to 4) as Integer |
LBound and UBound | Functions to get the lower and upper bounds of an array | LBound(arr) and UBound(arr) |
Preserve | Keeps existing values in a dynamic array when resized. | ReDim Preserve arr(1 To 20) |
Indexing | Array elements accessed using their index. Default base is 0 unless Option Base 1 is used. | arr(2) = 15 |
IsArray Function | Checks if a variable is an array | If IsArray(arr) Then MsgBox “It is an array” |
Iteration | Loops through array elements using For…Next or For Each | For i = LBound(arr) To UBound(arr) |
Key Concepts of VBA Arrays
- Declaring an Array: To declare an array, you use the Dim statement along with the parentheses to indicate the number of elements in the array.
Syntax:
Dim arr(5) As Integer ' Declares an array with 5 elements (0 to 4)
2. Array Indexing: In VBA, arrays are zero-based by default, meaning that the first element is at index 0. You can access each element using its index.
VBA Code for Array Indexing:
Arr(0)=10 ‘assigns value 10 to the first element Arr(1)=20 ‘Assigns value 20 to the second element
4. Types of Arrays:
-
- Fixed-Size Array: The size of the array is defined at the time of declaration and cannot be changed during execution.
VBA Code:
Arr(0)=10 ‘Assings value 10 to the first element Arr(1)=20 ‘ Assigns value 20 to the second element
-
- Dynamic Array: The size of the array can be changed during execution using the ReDim statement.
Syntax:
Dim arr() As Integer ' Declares a dynamic array ReDim arr(1 To 10) ' Resizes the array to have 10 elements
5. Multi-Dimensional Arrays: Arrays can have more than one dimension. For example, a two-dimensional array resembles a table or grid.
VBA Code:
Dim matrix(2, 3) As Integer ‘A 2D array with 3 rows and 4 columns
6. Using ReDim to Resize Arrays: Dynamic arrays can be resized with the ReDim keyword, and if you want to preserve the current data while resizing, you use ReDim Preserve.
Dim arr() As Integer ReDim arr(3) Arr(0)=1 Arr(1)=2 ReDim Preserve arr(5) ‘Resizes the array to 6 elements and keeps existing data
7. Looping Through an Array: You can use a For loop to iterate through each element of an array.
Dim arr(2) As Integer Dim I As Integer For I = 0 To 2 Arr(i)=i*10 Next i
Example of Working with Arrays:
Here’s a VBA example that demonstrates how to declare, populate, and iterate through an array:
Sub examplearray() Dim arr(3) As Integer 'Declare an array in 4 elements Dim i As Integer 'Assign values to the array arr(0) = 10 arr(1) = 20 arr(2) = 30 arr(3) = 40 'loop through an array and display values For i = 0 To 3 MsgBox "Element " & i & "=" & arr(i) Next i End Sub
Q&A
- What is the difference between a static and a dynamic array in VBA?
Answer: A static array has a fixed number of elements defined at declaration and cannot be resized later. A dynamic array, on the other hand, is declared without specifying the size and can be resized using the ReDim statement.
2. How do you declare a dynamic array in VBA and resize it later?
Answer: Declare it without specifying the size, like
Dim myArray() As Integer , and later resize it using ReDim myArray(5).
3. What is the purpose of Preserve when resizing an array?
Answer: Preserve is used to keep the existing data when resizing a dynamic array. For example:
Redim Preserve myArray(10)
This keeps the existing elements and resizes the array to accommodate new values.
4. How do you create a multi-dimensional array in VBA?
Answer: Use multiple dimensions in the declaration, like Dim myArray(2, 3) As String, which creates a 3×4 table structure that can store strings.
5. How do you loop through an array in VBA?
Answer: You can use a For loop, iterating through the array indices. For a one-dimensional array:
Dim I as Integer For i=LBound(myArray) To UBound(myArray) Debug.Print myArray(i) Next i
LBound and UBound are used to determine the lower and upper bounds of the array, respectively.
Other Related Topic
- VBA String Operator : In VBA, the primary string operator is the concatenation operator (&), which is used to join two or more strings together. There is also the + operator, which can also be used for concatenation but is less common due to potential ambiguity when dealing with numeric and string types.
- VBA Arithmetic Operators : VBA (Visual Basic for Applications) provides several arithmetic operators to perform basic mathematical operations on numerical values. Here are the main VBA arithmetic operators
External Link for More Information: