Repeat for each element of an array or collection
When you want to define actions that apply to all the elements in an array or to all the items in a collection, you place them in a For Each loop:
Syntax
Dim <variable>
For Each <variable> In <collection>
<statements>
Next
Parameters
<variable>
A temporary variable that will point to each element in the collection in turn.
<collection>
The name of the element collection to which the statements in the loop should be applied.
<statements>
The statements to be applied to each element in the collection.
Example
As an example, suppose you are working on a general community survey for a local district council. The council is interested in gaining as much information as possible about as many aspects of local life as possible, but does not want to put people off by having interviews take too long. It has been agreed that the questionnaire will be broken down into sections on various topics such as health, leisure, business, transport, and so on, and respondents will be invited to answer questions in up to three sections of their choice (or you could write the script so that three sections are chosen at random).
The metadata section of the interview script defines all the questions that can be asked. The routing section groups them into sections and displays the questions in the sections that the respondent has chosen. All respondents end the interview by answering some demographic questions. Here is an outline of the routing section.
WhichSections.Ask()
Dim Chosen
For Each Chosen In WhichSections
Select Case Chosen
Case {Health}
...
Case {Leisure}
...
Case {Business}
...
Case ...
...
End Select
Next
DemographicsPage.Ask()
Jumping out of a For loop
If you need to jump out of a loop before it has finished executing, insert anExit For statement at the point you want to leave the loop. When the interviewing program reaches this statement, it skips out of the loop to the statement immediately after Next. The example earlier in this section shows how this works.
See also