For Each . . . Next Statement
statements] [Exit For] [
Data Type: Variant
A variable to which the current element from the group is assigned.
A collection or an array.
A line or lines of program code to execute within the loop.
Loops through the items of a collection or the elements of an array.
block is executed only if
contains at least one element.
statements are executed for
group in turn until either there are
no more elements in
group, or the
loop is exited prematurely using the
For statement. Program execution then
continues with the line of code following
Each...Next loops can be nested, but
element must be unique. For
For Each myObj In anObject For Each subObject In myObject sName(ctr) = subObject.NameProperty ctr = ctr + 1 Next Next
uses a nested
Each...Next loop, but two
Any number of
For statements can be placed
Each...Next loop to allow for
conditional exit of the loop prematurely. On exiting the loop,
execution of the program continues with the line immediately
For example, the following loop terminates once the program
finds a name in the
that has fewer than 10 characters:
For Each subObject In myObj SName = subObject.NameProperty If Len(Sname) ...