Wa0030

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 21

Arrays

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.

Dim arrayname(lowerbound To UpperBound) As DataType


#1) Dim MyArrayExample(0 To 3) As Integer
#2) Dim MyArray(3) As String
#3) Dim MyArray(13 to 15) As Double
One Dimensional Array
Private Sub arrayExample1() Private Sub arrayExample3()
Dim firstQuarter(0 To 2) As String ‘creates array with index 0,1,2 Dim thirdQuarter(13 To 15) As String ‘creates
firstQuarter(0) = "Jan" array with index 13,14,15
firstQuarter(1) = "Feb" thirdQuarter(13) = "July"
firstQuarter(2) = "Mar" thirdQuarter(14) = "Aug"
MsgBox "First Quarter in calendar " & " " & firstQuarter(0) & " thirdQuarter(15) = "Sep"
" & firstQuarter(1) & " " & firstQuarter(2) MsgBox "Third Quarter in calendar " & " " &
End Sub thirdQuarter(13) & " " & thirdQuarter(14) & " " &
thirdQuarter(15)
Private Sub arrayExample2() End Sub
Dim secondQuarter(2) As String ‘creates array with index 0,1,2
secondQuarter(0) = "April"
secondQuarter(1) = "May"
secondQuarter(2) = "June"
MsgBox "Second Quarter in calendar " & " " &
secondQuarter(0) & " " & secondQuarter(1) & " " &
secondQuarter(2)
End Sub
One Dimensional Array
Option Explicit

Public Sub ArrayVarible()


Dim shet As Worksheet
Set shet = ThisWorkbook.Worksheets("Sheet1")

Dim Employee(1 To 6) As String


Dim i As Integer
For i = 1 To 6
Employee(i) = Range("A" & i).Value
Debug.Print Employee(i)
MsgBox ("Students name are " & "A" & i)
Next I
End Sub
Sub VariantArray()
Sub ArrayExample2() Dim arrVar() As Variant
Dim strNames(1 To 10) As String
Dim i As Long 'Define Values (Size = 0,1,2,3)
arrVar = Array(1, 2, 3, 4)
For i = 1 To 10
strNames(i) = Cells(i, 1).Value 'Change Values (Size = 0,1,2,3,4)
Next i arrVar = Array("1a", "2a", "3a", "4a", "5a")
End Sub
'Output Position 4 ("5a")
MsgBox arrVar(4)

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. 

• For example, the number of days in a week, you can


create an array with lower bound 0 and upper bound 6
and be sure that you will never change its size.
Dynamic Array
Dynamic Arrays allow us to resize the array during the run time. These are useful when you are not sure about the
size of the array. Suppose in college admission, you may not sure of how many students will actually get the
admission, so you can’t determine the size at the design or declaration time.

Declaration of a Dynamic array is similar to a Static array with empty parentheses.


Dim Employee() As String Sub dynamicArray()
Dim dynArray() As String
Dim curdate As Date
curdate = Now

ReDim dynArray(2) ‘ Redim will help to change the array size


during runtime
dynArray(0) = "John"
dynArray(1) = "Tom"
dynArray(2) = "Tonny"

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(3) ‘ Redim will reinitialise the array and


destroy the old values
dynArray(3) = "John"
MsgBox "Students Enrolled untill " & curdate & " are " &
dynArray(0) & ", " & dynArray(1)
& ", " & dynArray(2) & " , " & dynArray(3)
End Sub
Redim Preserve Sub preserveExample()
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 preserve dynArray(3) ‘ Redim preserve will retain the old


values
dynArray(3) = "John"
MsgBox "Students Enrolled untill " & curdate & " are " &
dynArray(0) & ", " & dynArray(1)
& ", " & dynArray(2) & " , " & dynArray(3)
End Sub
Multidimensional Array Sub ArrayEx_3d()
Dim strNames(1 To 5, 1 To 10, 1 To 3) As String
Dim i As Long, j As Long, k As Long

'Assign Values to Array


For k = 1 To 3
For i = 1 To 5
For j = 1 To 10
strNames(i, j, k) = Sheets("Sheet" & k).Cells(i, j).Value
Next j
Next i
Next k

'Output Array Values to Range


For k = 1 To 3
For i = 1 To 5
For j = 1 To 10
Sheets("Output" & k).Cells(i, j).Value = strNames(i, j, k)
Next j
Next i
Next k
End Sub
Sub eraseExample()
Erase Dim NumArray(3) As Integer
Dim decArray(2) As Double
Dim strArray(2) As String

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

' All values are erased.


MsgBox " Values after Erase " & NumArray(0) & "," & decArray(1) & " , " &
strArray(1)
End Sub
IsArray
Sub isArrayTest()
Dim arr1, arr2 As Variant
arr1 = Array("Jan", "Feb", "Mar")
arr2 = "12345"

MsgBox ("Is arr1 an Array : " & IsArray(arr1))


MsgBox ("Is arr2 an Array : " & IsArray(arr2))
End
 Join

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"

Sheet1.Cells(1, 1).Value = "My Favorite Beverages"


Sheet1.Cells(2, 1).Value = Drinks(1)
Sheet1.Cells(3, 1).Value = Drinks(2)
Sheet1.Cells(4, 1).Value = Drinks(3)
Sheet1.Cells(5, 1).Value = Drinks(4)
End Sub
• Write a Macro to do the addition first 5 numbers
• Write a code for printing fibonicci
Sub anyName()
Dim num as Integer, sum as long, I as long
Num= inputbox(“Enter number up to which you want addition of
number”,,0)
For i=1 to num
Sum=sum+i
Next i
Msgbox(“Sum of number- “&sum) End sub

You might also like