VBA Do While Loop – Using Do While…Loop in VBA

1 Star2 Stars3 Stars4 Stars5 Stars (1 votes, average: 5.00 out of 5)
Loading...

In this VBA Do While / VBA Do Until tutorial I will demonstrate how to use conditional loops i.e. loops that run While or Until a condition is met. Do While loops should not be used to iterate through arrays or collections. Instead they are met to be run only if a specific condition needs to be met during execution (e.g. run my code while my condition is met). If you want to learn how to iterate through VBA Arrays or VBA Collections read my VBA For loop tutorial.

VBA Do While

Below is an example of a VBA Do While loop. The loop will run While the condition i<10 in the statement below is true.

Or you can push the While statement to the end of the loop:

Why push the While statement to the back. This makes sense if you don’t want the condition to be tested in the first run of the loop.

Do Until Loop

Will loop until the condition in the Until statement is met (true).

Or you can push the Until statement to the end of the loop:

The For loop and For Each loop should be used in different situations! Be sure to read this post

.

Exit Do While / Do Until loop

Sometimes it makes sense to leave a VBA Do While or a VBA Do Until loop earlier than the condition specified. We can do this using the Exit Do statement. Below usage:

See this example below of how Exit Do is used: