Arrays

Arrays represent a group of variables of the same type. Since they often contain values that share common attributes, it is easier for us to assign them a unique name, and then to access each of them with an index number in a array. The index of the first member of an array is usually 0, and the index of the last one is smaller for one than the size of the array. It is possible to work with a whole array or with its individual members, they can be one or multi-dimensional …

We declare arrays as follows:

Dim <array name> (<size>[,<size 2>,…,<size n>] As <data type>

The first member of the array has an index of 0. It is possible to start the numbering of the array from number 1. In this case, at module level, the following declaration must be entered:

Option Base 1

Let’s see how to declare and use arrays. For example, if we want to declare a simple array containing names of the months within a year and to write a program that shows the name of the shortest month of the year, we will write following code:

Sub MonthArray()

Dim MonthArr(11) As String

MonthArr(0) = “January”
MonthArr(1) = “February”
MonthArr(2) = “March”
MonthArr(3) = “April”
MonthArr(4) = “May”
MonthArr(5) = “June”
MonthArr(6) = “July”
MonthArr(7) = “August”
MonthArr(8) = “September”
MonthArr(9) = “October”
MonthArr(10) = “November”
MonthArr(11) = “December”

MsgBox “Shortest month of the year:” & MonthArr(1)

End Sub

If we wanted to show all the members of an array, it’s enough to add a few more lines o the end of the program:

For i = 0 To 11
 MsgBox (MonthArr(i))
Next i

How to create multidimensional array? Two dimensional array (Matrix) is declared in following way:

Dim MonthArr (12,12) As String

Further, to access each individual member of an array should enter two index numbers within brackets.