10/03/2012

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

 
Copyright 2009 360 graus