Microsoft Office Tutorials and References
In Depth Information
FOR EACH-NEXT LOOPS
a 10 x 10 x 10 array with the value –1. When the three loops finish executing, each
of the 1,000 elements in MyArray contains –1.
Dim MyArray(1 To 10, 1 To 10, 1 To 10)
For i = 1 To 10
For j = 1 To 10
For k = 1 To 10
MyArray(i, j, k) = -1
Next k
Next j
Next I
FOR EACH-NEXT LOOPS
Your procedures often need to loop various items in a collection . A collection is a
group of related objects. For example, the Workbooks collection includes all the
workbooks currently open. The Charts collection comprises all chart sheets in a
workbook. The Sheets collection includes all worksheets and chart sheets. All
series in a chart are included in the SeriesCollection collection.
The following example displays the names of all series in the active chart, each
in a separate message box:
Sub ShowSeriesNames()
Dim s As Series
For Each s In ActiveChart.SeriesCollection
MsgBox s.Name
Next s
End Sub
In the preceding example, s is a variable name. There’s nothing special
about this name ; you can replace it with any valid variable name.
The following example loops through all cells in a range and displays the sum:
Sub AddCells()
Dim c As Range
Dim Total As Double
Total = 0
For Each c In Range(“A1:C100”)
Total = Total + c
Next c
MsgBox Total
End Sub
Search JabSto ::




Custom Search