Before you use an array, you must declare it.
An array is declared with a Dim or a Public statement, just like a regular variable.
However you also must specify the number of elements in the array by
specifying the first index number, the keyword to, and the last index number, all within the parentheses.
For example, if a certain lecture has a total of 65 students
Dim Score(1 to 65) As IntegerThis sets aside 65 subscripted variables, each is capable of storing an integer representing the score for that student.
Score(1) = 67 Score(2) = 83 ...Assuming that we have filled the array Score with the score of each of the 65 students in the class, we can then compute the average score using a loop as follows:
Dim average As Double, sum As Double Dim count As Integer Dim n As Integer sum = 0.0 count = 65 For n = 1 To count sum = sum + Score(n) Next n average = sum / count MsgBox "The class average is: " & averageIf only the upper index number is specified in declaring an array, then VBA assumes the default lower index number of 0, that is the first element of the array has an index of 0. Many modern languages like C and C++ actually require that the first element of an array is indexed by 0.
Dim MyArray(100) As Double Dim MyArray(0 to 100) As DoubleIn languages like Fortran, the first element of an array is indexed by 1 rather than by 0 (an index of 0 is not allowed). In VBA, the default lower index number can be changed to 1 globally by including the following statement in the Declarations section of your module
Option Base 1In that case the following declaration are equivalent and gives a 100-element array of doubles.
Dim MyArray(100) As Double Dim MyArray(1 to 100) As DoubleIn VBA, indices can even be negative integers.