Excel VBA Macros: Mastering Arrays for Efficient Data Handling

Arrays are the unsung heroes of Excel VBA, offering a powerful way to manage data efficiently. In this guide, we’ll explore the world of arrays, from one-dimensional structures to dynamic arrays and user-defined functions, unlocking their potential for handling data.

One-dimensional Array

Create one-dimensional arrays effortlessly with these steps:

  1. Open the Visual Basic Editor.
  2. Place a command button on your worksheet and add the following code lines:
Dim Films(1 To 5) As String

Films(1) = "Lord of the Rings"
Films(2) = "Speed"
Films(3) = "Star Wars"
Films(4) = "The Godfather"
Films(5) = "Pulp Fiction"

MsgBox Films(4)

Result: When you click the command button, you’ll see “The Godfather” displayed in a message box.

Element of a One-dimensional Array in Excel VBA

Two-dimensional Array

If you’re dealing with two-dimensional data, follow these steps:

  1. Open the Visual Basic Editor.
  2. Place a command button on your worksheet and add the following code lines:
Dim Films(1 To 5, 1 To 2) As String
Dim i As Integer, j As Integer

For i = 1 To 5
    For j = 1 To 2
        Films(i, j) = Cells(i, j).Value
    Next j
Next i

MsgBox Films(4, 2)

Result: Click the command button to display an element from the array.

Element of a Two-dimensional Array in Excel VBA

Dynamic Array

When you need arrays that can change size dynamically, use the ReDim keyword:

  1. Open the Visual Basic Editor.
  2. Place a command button on your worksheet and add the following code lines:
Dim numbers() As Integer, size As Integer, i As Integer

size = WorksheetFunction.CountA(Worksheets(1).Columns(1))
ReDim numbers(size)

For i = 1 To size
    numbers(i) = Cells(i, 1).Value
Next i

MsgBox numbers(size)

Dynamic Array in Excel VBA

Result: Your dynamic array adapts to the size of your data.

Last Element of the Array

Array Function

Use the Array function to initialize an array efficiently:

  1. Open the Visual Basic Editor.
  2. Place a command button on your worksheet and add the following code lines:
Dim departments As Variant

departments = Array("Sales", "Production", "Logistics")

MsgBox departments(0) ' Default array indexing

Result: You’ll see the first element, “Sales,” in a message box.

Array Function Result

User Defined Function

Create a User Defined Function (UDF) to return month names using the Array function:

  1. Open the Visual Basic Editor and insert a module.
  2. Add the following code:
Function MONTHNAMES()
    MONTHNAMES = Array("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec")
End Function

Result: You can use the function =MONTHNAMES() to get the month names.

Month Names in Excel VBA

Size of an Array

Determine the size of an array in Excel VBA using UBound and LBound:

  1. Open the Visual Basic Editor.
  2. Place a command button on your worksheet and add the following code lines:
Dim Films(1 To 5, 1 To 2) As String, x As Integer, y As Integer

x = UBound(Films, 1) - LBound(Films, 1) + 1
y = UBound(Films, 2) - LBound(Films, 2) + 1

MsgBox "This array consists of " & x * y & " elements"

Size of an Array in Excel VBA

Result: The message box shows the total number of array elements.

Size of an Array Result

Mastering arrays is essential for effective data handling in Excel VBA. Whether you’re working with one-dimensional or two-dimensional data, dynamic arrays, or creating custom functions, arrays are your key to efficient data management.