Skip to Content
Course content

8.1 Working with Arrays: Static, Dynamic, and Multi-Dimensional.

In VBA, arrays are used to store multiple values in a single variable. Arrays can be particularly useful for handling large sets of data or performing repetitive operations on similar items. There are different types of arrays in VBA, each with its own use case: Static Arrays, Dynamic Arrays, and Multi-Dimensional Arrays.

1. Static Arrays

A Static Array has a fixed size, defined at the time of its declaration. Once declared, the size of the array cannot be changed during the execution of the program. Static arrays are typically used when you know the exact number of elements in advance.

Declaring Static Arrays:

Dim arr(5) As Integer ' Array with 6 elements (0 to 5)

In this example, arr is a static array with 6 elements (index 0 to 5). The size is defined when the array is declared and cannot be changed later.

Accessing Elements:

arr(0) = 10 ' Assigns value 10 to the first element
arr(5) = 20 ' Assigns value 20 to the last element

You access elements in the array by using the index. The index starts from 0, so arr(0) refers to the first element, and arr(5) refers to the sixth element.

2. Dynamic Arrays

A Dynamic Array does not have a fixed size. You can change the size of the array during runtime using the ReDim keyword. Dynamic arrays are particularly useful when you don't know in advance how many elements you need.

Declaring Dynamic Arrays:

Dim arr() As Integer ' Dynamic array with no size

Initially, the array has no size. You can use ReDim to specify the size of the array later.

Resizing the Array:

ReDim arr(5) ' Resizes the array to 6 elements (0 to 5)

You can change the size of the array using ReDim as needed. However, resizing an array with ReDim will erase the existing data. To preserve the data, use the Preserve keyword.

Example: ReDim with Preserve

ReDim Preserve arr(10) ' Increases the size of the array to 11 elements

In this example, the array size is increased, and the existing data in the array is preserved.

3. Multi-Dimensional Arrays

A Multi-Dimensional Array is an array with more than one dimension, such as a 2D array (like a matrix) or a 3D array. These arrays are useful when you need to store data in a tabular or matrix-like structure.

Declaring Multi-Dimensional Arrays:

You can declare multi-dimensional arrays by specifying multiple indices in the Dim statement.

Dim arr(2, 3) As Integer ' 2D Array with 3 rows and 4 columns (indices 0 to 2 for rows and 0 to 3 for columns)

In this example, arr is a 2D array with 3 rows and 4 columns, giving it a total of 12 elements.

Accessing Elements in a Multi-Dimensional Array:

arr(0, 1) = 5  ' Assigns 5 to the element at the first row, second column
arr(2, 3) = 10 ' Assigns 10 to the element at the third row, fourth column

You can access elements in a multi-dimensional array by specifying each index in the correct order.

4. Examples of Working with Arrays

Example 1: Static Array

Sub StaticArrayExample()
    Dim arr(4) As Integer ' Static array with 5 elements (0 to 4)
    arr(0) = 10
    arr(1) = 20
    arr(2) = 30
    arr(3) = 40
    arr(4) = 50

    For i = 0 To 4
        Debug.Print arr(i) ' Prints each element of the static array
    Next i
End Sub

Example 2: Dynamic Array

Sub DynamicArrayExample()
    Dim arr() As Integer ' Dynamic array
    ReDim arr(4) ' Initially resize to 5 elements (0 to 4)
    arr(0) = 10
    arr(1) = 20
    arr(2) = 30
    arr(3) = 40
    arr(4) = 50

    ' Resize the array to 8 elements
    ReDim Preserve arr(7)
    arr(5) = 60
    arr(6) = 70
    arr(7) = 80

    For i = 0 To 7
        Debug.Print arr(i) ' Prints each element of the dynamic array
    Next i
End Sub

Example 3: Multi-Dimensional Array (2D)

Sub MultiDimensionalArrayExample()
    Dim arr(2, 2) As Integer ' 2D array with 3 rows and 3 columns

    arr(0, 0) = 1
    arr(0, 1) = 2
    arr(0, 2) = 3
    arr(1, 0) = 4
    arr(1, 1) = 5
    arr(1, 2) = 6
    arr(2, 0) = 7
    arr(2, 1) = 8
    arr(2, 2) = 9

    For i = 0 To 2
        For j = 0 To 2
            Debug.Print arr(i, j); ' Print each element in the 2D array
        Next j
    Next i
End Sub

5. Best Practices for Working with Arrays

  • Use Dynamic Arrays: When the number of elements is unknown or can change, use dynamic arrays for flexibility.
  • ReDim with Preserve: When resizing dynamic arrays, use ReDim Preserve to retain existing values.
  • Limit Array Size: Be mindful of the array size to avoid performance issues. If necessary, break down large arrays into smaller chunks.
  • Multi-Dimensional Arrays: Use multi-dimensional arrays for handling tabular or matrix-like data, such as grids in Excel.

6. Summary of Array Types

Array Type Size When to Use
Static Array Fixed size, defined at declaration When the number of elements is known in advance.
Dynamic Array Flexible size, resized during runtime When the number of elements is unknown or can change.
Multi-Dimensional Array Multiple dimensions (e.g., 2D, 3D) When you need to store and manipulate data in a table, matrix, or grid structure.

By understanding and working with different types of arrays in VBA, you can effectively manage and manipulate large sets of data, improving the efficiency and flexibility of your code.

Commenting is not enabled on this course.