Microsoft Office Tutorials and References
In Depth Information
You can also execute statements repeatedly as long as a condition is False by using the Until
Do Until condition
When program execution reaches the Do statement, condition is evaluated. If it is True (if using
While ) or False (if using Until ), the statements are executed and then condition is evaluated
again. This continues until the value of condition changes. Depending on the initial value of
condition , the statements in the loop may not be executed even once.
You can also write a Do...Loop statement that tests condition at the end of the loop. As
before, you can use either While or Until :
Loop While condition
Loop Until condition
When you use this syntax, the statements are executed once and then condition is evaluated.
By testing condition at the end of the loop, you ensure that the statements in the loop will be
executed at least once.
To exit a Do...Loop early, use the Exit Do statement.
Using Conditional Statements
VBA’s conditional statements are used to execute or not execute a block of statements depending on a
program condition. The statements are executed once or not at all. There are two conditional
statements in VBA.
The If...Then...Else statement executes a block of statements if a specified condition is True.
Optionally, a second block of statements is executed if the condition is False. The syntax is
If condition Then