You can fill elements of an array very easily. You can use the following syntax:
ArrayName(Element) = Value
Where element is a valid element. The following code declares gArray and fills elements 1,2 and 3 with names.
Dim sArray(1 To 10) As String
sArray(1) = "Paul"
sArray(2) = "Fred"
sArray(3) = "Colin"
Likewise, you can access the value in the array in the same way. The following code displays a message box containing the 3rd value in sArray:
Msgbox sArray(3)
As I said at the start of this tutorial, this can be very beneficial as you can access different values in the array using code. For example, if you hard coded 3 variables:
Dim sName1 As String
Dim sName2 As String
Dim sName3 As String
you would be limited to 3 names, and if you wanted to access them, you would have to write different code for accessing each variable, as you can't type
Msgbox (sName & lNum)
to access sName1, if lNum is 1. With Arrays, you can:
Msgbox sNames(lNum)
Comments