While...Wend Statement

Description

Executes a series of statements as long as a given condition is True.

Syntax

While condition
[statements]
Wend

The While...Wend statement syntax has these parts:

Part

Description

condition

Numeric or string expression that evaluates to True or False.

statements

One or more statements executed while condition is True.


Remarks

If condition is True, all statements in statements are executed until the Wend statement is encountered. Control then returns to the While statement and condition is again checked. If condition is still True, the process is repeated. If it is not True, execution resumes with the statement following the Wend statement.

While...Wend loops can be nested to any level. Each Wend matches the most recent While.

Caution

Do not branch into the body of a While...Wend loop without executing the While statement. Doing so may cause run-time errors or other problems that are difficult to locate.

Tip

The Do...Loop statement provides a more structured and flexible way to perform looping.

See Also

Do...Loop Statement, With Statement.

Example

This example uses the While...Wend statement to increment a counter variable. The statements in the loop are executed as long as the condition evaluates to True.


Counter = 0    ' Initialize variable.
While Counter < 20    ' Test value of Counter.
    Counter = Counter + 1    ' Increment Counter.
Wend
Debug.Print Counter    ' Prints 20 in Debug window.