Array

 

An Excel VBA array is a group of variables. You can refer to a specific variable (element) of an array by using the array name and the index number. To create a one-dimensional array, execute the following steps.

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)

1. The first code line declares a String array with name Films. The array consists of five elements.

2. Next, we initialize each element of the array. In other words: we assign beginning values.

3. Finally, we display the fourth film using a MsgBox.

Exit the Visual Basic Editor and test the array.

Result when you click the command button on the sheet:

The Fourth Element of the Excel VBA Array

Did you find this information helpful? Show your appreciation, vote for us.

Go to Top: Array    |    Go to Next Topic: Date and Time

Learn more about arrays, Login to the right >>
100 easy to follow Excel VBA examples. Limited time-offer: $39.00 but only $29.00. Ends on 31 May.