spot_img

VBA Do While Loop

The Do While Loop is used when we want to repeat a set of statements as long as the condition is True and stops when the condition turns into False.

The condition can be checked either at the start or at the end of the loop. “The Do While … Loop Statements” check the condition at the start of the loop.

And “The Do … Loop While Statements” check the condition at the end of the Loop.

If the condition is checked at the start of the Loop, the block of code does not execute. If the condition is not met at the beginning and loop does not run even once whereas if the condition is checked at the end, the Loop runs atleast once.

Syntax

Do While… loop statement

It checks the condition at the stating of the loop.

Do… While loop statement

There is an alternate Syntax for Do…while loop. It checks the condition at the ending of the loop.

Flow Diagram

VBA Do While Loop

Example 1

Both the above syntax is explained with the help of the examples. Such as,

The below example uses Do While… loop to check the condition at the start of the loop. The statements inside the loop will be executed, only when the condition becomes True.

After executing the code, you will get the following output.

VBA Do While Loop

Example 2

In this example, we use Do…while loop to check the condition at the end of the loop. The statements inside the loop should execute at least once, even if the condition is False.

After executing the code, you will get the following output.

VBA Do While Loop


Next TopicVBA Do Until Loop

spot_img
Previous articleVBA Data Types
Next articleCircular reference in Excel