Skip to content

Latest commit

 

History

History
57 lines (37 loc) · 2.02 KB

whilewend-statement.md

File metadata and controls

57 lines (37 loc) · 2.02 KB
title keywords f1_keywords ms.assetid ms.date ms.localizationpriority
While...Wend statement (VBA)
vblr6.chm1008811
vblr6.chm1008811
c905a6a3-fa70-42df-5ef0-c4e3193c2e10
12/03/2018
medium

While...Wend statement

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 Required. Numeric expression or string expression that evaluates to True or False. If condition is Null, condition is treated as False.
statements Optional. One or more statements executed while condition is True.

Remarks

If condition is True, all 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.

Tip

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

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.

Dim Counter 
Counter = 0 ' Initialize variable. 
While Counter < 20 ' Test value of Counter. 
 Counter = Counter + 1 ' Increment Counter. 
Wend ' End While loop when Counter > 19. 
Debug.Print Counter ' Prints 20 in the Immediate window. 

See also

[!includeSupport and feedback]