For Each element In group [statements] [Exit For] [statements] Next [element]
Parameters
Remarks
The For...Each block is entered if there is at least one element in group. Once the loop has been entered, all the statements in the loop are executed for the first element in group. If there are more elements in group, the statements in the loop continue to execute for each element. When there are no more elements in group, the loop is exited and execution continues with the statement following the Next statement.
Any number of Exit For statements may be placed anywhere in the loop as an alternative way to exit. Exit For is often used after evaluating some condition, for example If…Then, and transfers control to the statement immediately following Next.
You can nest For...Each...Next loops by placing one For...Each...Next loop within another. However, each loop element must be unique.
Note If you omit element in a Next statement, execution continues as if element is included. If a Next statement is encountered before its corresponding For statement, an error occurs.
Do...Loop Statement | Exit Statement | For...Next Statement | Making Faster For...Next Loops | Using Do...Loop Statements | Using For Each...Next Statements | Using For...Next Statements | While…End While Statement