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:
- Open the Visual Basic Editor.
- 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.
Two-dimensional Array
If you’re dealing with two-dimensional data, follow these steps:
- Open the Visual Basic Editor.
- 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.
Dynamic Array
When you need arrays that can change size dynamically, use the ReDim keyword:
- Open the Visual Basic Editor.
- 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)
Result: Your dynamic array adapts to the size of your data.
Array Function
Use the Array function to initialize an array efficiently:
- Open the Visual Basic Editor.
- 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.
User Defined Function
Create a User Defined Function (UDF) to return month names using the Array function:
- Open the Visual Basic Editor and insert a module.
- 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.
Size of an Array
Determine the size of an array in Excel VBA using UBound and LBound:
- Open the Visual Basic Editor.
- 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"
Result: The message box shows the total number of array elements.
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.