Declaring an array

An array is declared in a similar way to any other variable. The only difference is that the size (or Dimension) of the array must be written in parentheses after the variable name.

Dim NameOfArray (ArrayLength) As DataType

The following example declares two arrays. In the first, for an array called expenses, each of the five elements is a Currency variable. In the second, for an array called employee, each of the 20 elements is a String variable.

Dim expenses (5) As Currency

'expenses is declared as a five-element currency array variable Dim employee(20) As String

'employee is declared as a twenty-element long String variable

An array can be populated with a series of values by using the Array function. For example, the expenses array is populated as follows:

Expenses = Array (20.50, 35.50, 38.25, 65.40, 54.35)

An array can also be populated using a For.. .Next loop (see next example).

0 0

Post a comment