Page 108 - Excel Progamming Weekend Crash Course
P. 108
h540629 ch06.qxd 9/2/03 9:33 AM Page 83
Session 6 — Control Constructs 83
Stopping an Infinite Loop
When an infinite loop is running, you usually have to resort to the Windows
Task Manager to stop it. The steps are:
1. Press Ctrl+Alt+Del to open the Windows Security dialog box.
2. Click the Task Manager button to display the Task Manager dialog box.
3. On the Applications tab, select the Microsoft Visual Basic entry.
4. Click the End Task button.
This terminates the VBA Editor. Unsaved changes to your programs will be lost.
The loop repeats as long as condition is True. You can see that this is functionally
equivalent to the following Do variant:
Do While condition
...
Loop
While...Wend is support only for backward compatibility. You should not use it in your
programs.
The For...Next Statement
The For...Next statement repeats a block of code a specified number of times. Its simplest
form is:
For index = start To stop
...
Next index
Index is a variable that is used to count loop repetitions. It is usually declared to be
type Integer.
Start is an expression specifying the starting value of index.
Stop is an expression specifying the ending value of index.
When a For...Next statement is encountered, index is set equal to start, and the
statements between the For and the Next are executed. Index is incremented by 1, and the
process repeats. The statements are repeated until index is greater than stop. Note that for
the last repetition, index is equal to stop.
Including the name of the counter variable in the Next statement is optional. If you sim-
ply use Next alone, VBA automatically matches it with the previous For.