Sei sulla pagina 1di 3

SAMPLE SCRIPT ARRAY – INDOTRAININGCENTER.

COM

Option Base 1 'Forces arrays to begin at 1 (not zero)


Sub WhatLifeWouldBeLikeWithoutArrays()
'Declare the variables
Dim MyVar1 As String
Dim MyVar2 As String
Dim MyVar3 As String
Dim MyVar4 As String
Dim MyVar5 As String
Dim MyVar6 As String
Dim MyVar7 As String
'Read the data
MyVar1 = Range("A1").Value
MyVar2 = Range("A2").Value
MyVar3 = Range("A3").Value
MyVar4 = Range("A4").Value
MyVar5 = Range("A5").Value
MyVar6 = Range("A6").Value
MyVar7 = Range("A7").Value
'Write the data
Range("B1").Value = MyVar1
Range("B2").Value = MyVar2
Range("B3").Value = MyVar3
Range("B4").Value = MyVar4
Range("B5").Value = MyVar5
Range("B6").Value = MyVar6
Range("B7").Value = MyVar7
End Sub
Sub Arrays1()
'A simple one dimensional Static array
'Declare the variables
'Note, the 7 is NOT the number of elements, it is the UPPER BOUND
Dim MyArrayA(7) As String 'Set up the Array
Dim MyCounter As Integer 'A simple counter for looping
'Select the correct sheet...
'Note No need to select start of range as...
'...we're using the Cells object
ShSimple.Select
'Clear old written data
Range("D:D").ClearContents
'Loop to read data in to the ArrayA
For MyCounter = 1 To 7
'Load the data (increments with each pass through the loop)
MyArrayA(MyCounter) = Cells(MyCounter, 1).Value
Next
'Loop to write data from the ArrayA
For MyCounter = 1 To 7
Cells(MyCounter, 4).Value = MyArrayA(MyCounter)
Next
End Sub
-

Sub Arrays2()
'A simple one dimensional Dynamic array
'Declare the variables
'Note, the 7 is NOT the number of elements, it is the UPPER BOUND
Dim MyArrayB() As String 'Set up the array
Dim MyCounter As Integer 'A simple counter for looping
Dim MyRowCount As Long 'Holds the count of rows with data
'Select the correct sheet...
'Note No need to select start of range as...
'...we're using the Cells object
SAMPLE SCRIPT ARRAY – INDOTRAININGCENTER.COM

ShSimple.Select
'Clear old written data
Range("D:D").ClearContents
'Find the row number of the last populated cell
MyRowCount = Range("A1048576").End(xlUp).Row
'Redimension the array (now we know how big it should be)
ReDim MyArrayB(MyRowCount)
'Loop to read data in to the array
For MyCounter = 1 To MyRowCount
'Load the data (increments with each pass through the loop)
MyArrayB(MyCounter) = Cells(MyCounter, 1).Value
Next
'Loop to write data from the array
For MyCounter = 1 To MyRowCount
Cells(MyCounter, 4).Value = MyArrayB(MyCounter)
Next
'Destroy the array (Deallocate)
Erase MyArrayB
End Sub
Sub Arrays3()
'A simple two dimensional Static array
'Declare the variables
'Note, the 7 is NOT the number of elements, it is the UPPER BOUND
Dim MyArrayC(7, 2) As Variant 'Dim the array
Dim MyOuterLoop As Integer 'A simple counter for looping
Dim MyInnerLoop As Integer 'A simple counter for looping
'Select the correct sheet...
'Note No need to select start of range as...
'...we're using the Cells object
ShSimple.Select
'Clear old written data
Range("D:E").ClearContents
'Loop to read data in to the array
For MyOuterLoop = 1 To 7
For MyInnerLoop = 1 To 2
'Load the data (increments on each pass through the loop)
MyArrayC(MyOuterLoop, MyInnerLoop) = _
Cells(MyOuterLoop, MyInnerLoop).Value
Next 'Inner loop
Next 'Outer loop
'Loop to write data in to the array

For MyOuterLoop = 1 To 7
For MyInnerLoop = 1 To 2
'Load the data (increments on each pass through the loop)
'Note the +3 gets us to column D
Cells(MyOuterLoop, MyInnerLoop + 3).Value = _
MyArrayC(MyOuterLoop, MyInnerLoop)
Next 'Inner loop
Next 'Outer loop
End Sub
Sub Arrays4()
'Reads a whole data table dynamically
'Declare the variables
Dim MyArrayD() As Variant 'The Array
Dim RowCount As Integer 'Holds the number of lines in the array
Dim ColCount As Integer 'Holds the number of columns in the array
'Load the array...
'Note the source data sheet is NOT selected...
'...it is specified before the range object
MyArrayD = ShSalesData.Range("A6").CurrentRegion
SAMPLE SCRIPT ARRAY – INDOTRAININGCENTER.COM

'Get the count of the rows...the FIRST Dimension of the array (needed to
write)
RowCount = UBound(MyArrayD)
'Get the count of the columns...the SECOND Dimension of the array (hence
,2)
ColCount = UBound(MyArrayD, 2)
'Select destination sheet by vbName
ShWriteHere.Select
'The range is specified using Cells
'Cells(1,1) means the first cell is A1...
'...and Cells(RowCount, ColCount) gives the last cell
Range(Cells(1, 1), Cells(RowCount, ColCount)) = MyArrayD
End Sub
Sub Arrays5()
'Reads a whole data table dynamically,
'...and returns a specific result to a message box
'Declare the variables
Dim MyArrayE() As Variant 'The Array
Dim RowCount As Integer 'Holds the number of lines in the array
Dim ColCount As Integer 'Holds the number of columns in the array
Dim Counter As Integer 'A simple counter
Dim MyCategory As String 'Holds the categor from the user
Dim MySum As Variant 'Holds the rolling sum
Dim MyResponse As Variant 'For the "Try again" message box
'Load the array...
'Note the source data sheet is NOT selected...
'...it is specified before the range object
MyArrayE = ShSalesData.Range("A6").CurrentRegion
'Get the count of the rows...the FIRST Dimension of the array (needed to
write)
RowCount = UBound(MyArrayE)
'Get the count of the columns...the SECOND Dimension of the array (hence
,2)
ColCount = UBound(MyArrayE, 2)
StartOver:
'Get the category from the user
MyCategory = LCase(InputBox("Which category would you like to sum?", _
"Select Category"))
'Loop to check for match
For Counter = 1 To RowCount
If LCase(MyArrayE(Counter, 5)) = MyCategory Then
MySum = MySum + MyArrayE(Counter, 7)
End If
Next Counter
'Check to see if a value was found
If MySum = 0 Then
MsgBox "No value found", vbCritical, "Invalid Selection"
End
End If
MyResponse = MsgBox("The total profit for " & MyCategory & " was:" _
& vbNewLine & Format(MySum, "Currency") & vbNewLine _
& "Would you like to look for another category?", _
vbYesNo, "Result Found!")
'reset MySum to zero
MySum = 0
If MyResponse = vbYes Then GoTo StartOver
End Sub
-

Potrebbero piacerti anche