Professional Documents
Culture Documents
Wa0030
Wa0030
Wa0030
Arrays
• Arrays are a special kind of variable that can store
multiple values of the same data type.
For example, if you have the names of 100 employees,
then instead of creating 100 variables of data type string,
you can just create one array variable of type string and
assign 100 values to the same array variable.
One Dimensional Array
• An array that has all the elements in a single row or in a
single column is called a One-dimensional array.
• Listing the names of all the students in the class in a
single column is an example of a one-dimensional array.
It is declared as shown below.
End Sub
Sub ArrayExample()
Dim strNames(1 to 4) as String Sub ArrayExample_1Line()
Dim strNames() As Variant
strNames(1) = "Shelly"
strNames(2) = "Steve" strNames = Array("Shelly", "Steve", "Neema", "Jose")
strNames(3) = "Neema"
strNames(4) = "Jose" End Sub
End Sub
Sub ArrayExample()
Dim strNames(1 to 4) as String
strNames(1) = "Shelly"
strNames(2) = "Steve"
strNames(3) = "Neema"
strNames(4) = "Jose"
End Sub
Range("A1").Value = strNames(1)
Range("A2").Value = strNames(2)
Range("A3").Value = strNames(3)
Range("A4").Value = strNames(4)
Two-Dimensional Array
Dim ArrayName(FirstIndex To LastIndex, FirstIndex To LastIndex)
As DataType.
Sub Twodim()
Dim totalMarks(1 To 2, 1 To 3) As Integer
totalMarks(1, 1) = 23
totalMarks(2, 1) = 34
totalMarks(1, 2) = 33
totalMarks(2, 2) = 55
totalMarks(1, 3) = 45
totalMarks(2, 3) = 44
Msgbox “Total Marks in Row 2 and column 2 is “
&totalMarks(2,2)
Msgbox “Total Marks in Row 1 and column 3 is “
&totalMarks(1,3)
End Sub
Variant Array
Store the various types of data like String, Date, Long, Integer in a single array
Sub arrayVariant()
Dim arrayData(3) As Variant
arrayData(0) = "Vikram Vikrant"
arrayData(1) = 411234567890#
arrayData(2) = 38
arrayData(3) = "06-09-1972"
MsgBox "Details of person" & arrayData(0) & " is " & " Phone
No " & arrayData(1) & " ,Id " & arrayData(2) & " ,DOB " &
arrayData(3)
End Sub
Fixed Arrays
• Fixed Arrays also called Static Arrays have a fixed lower
bound and upper bound and this size cannot be changed
at run time. The size of the array is specified during the
declaration within the parentheses. All the above
examples are Fixed arrays as we have mentioned the size
of it during the declaration.
• Fixed arrays are usually used when you are sure about the
size of the array.
MsgBox "Students Enrolled after " & curdate & " are “ &
dynArray(0) & ", " & dynArray(1) & ", " & dynArray(2)
End Sub
Sub RedimExample()
Dim dynArray() As String
Dim curdate As Date
curdate = Now
Dim size As Integer
ReDim dynArray(2)
dynArray(0) = "John"
dynArray(1) = "Tom"
dynArray(2) = "Tonny"
MsgBox "Students Enrolled untill " & curdate & " are " &
dynArray(0) & ", " & dynArray(1)
& ", " & dynArray(2)
ReDim dynArray(2)
dynArray(0) = "John"
dynArray(1) = "Tom"
dynArray(2) = "Tonny"
MsgBox "Students Enrolled untill " & curdate & " are " &
dynArray(0) & ", " & dynArray(1) & ", " & dynArray(2)
NumArray(0) = 12345
decArray(1) = 34.5
strArray(1) = "Erase Function"
Dim DynaArray()
ReDim DynaArray(3)
MsgBox " Values before Erase " & (NumArray(0)) & "," & (decArray(1)) & " , " &
(strArray(1))
Erase NumArray
Erase decArray
Erase strArray
Erase DynaArray ' Free the memory
Sub joinExample()
Dim Result As String
Dim dirarray(0 To 2) As String
dirarray(0) = "D:"
dirarray(1) = "SoftwareTestingHelp"
dirarray(2) = "Arrays"
Result = Join(dirarray, "\")
MsgBox "Date after joining " & Result
End Sub
Private Sub cmdLoadBeverages_Click()
Dim Drinks(1 To 4) As String
Drinks(1) = "Pepsi"
Drinks(2) = "Coke"
Drinks(3) = "Fanta"
Drinks(4) = "Juice"