VBA Do Until Loop

Last Updated :

-

Edited by :

Reviewed by :

Table Of Contents

arrow

What is Do Until Loop in VBA Excel?

In VBA Do Until Loop, we need to define criteria after the until statement which means when we want the loop to stop and the end statement is the loop itself. So if the condition is FALSE it will keep executing the statement inside the loop but if the condition is TRUE straight away it will exit the Do Until statement.

As the words itself says that to do some task until a criterion is reached, Do until the loop is used in almost all of the programming languages, in VBA also we sometimes use Do until loop. Do Until Loop means to do something until the condition becomes TRUE. It is like a logical function that works based on TRUE or FALSE.

vba do until condition

It is the opposite of the Do While Loop. It runs the Loops as long as the condition is TRUE.

Syntax

Do Until Loop has two kinds of syntax.

Syntax #1 

Do Until 



Loop

Syntax #2

Do



Loop Until 

Both look very similar. There is one simple differentiation.

In the first syntax, the Do Until Loop checks the condition and gets the result TRUE or FALSE. If the condition is FALSE, it will execute the code and perform a specified task, and if the condition is TRUE, then it will exit the Loop.

In the second syntax, the Do Loop, firstly, it will execute the code task and then test whether the condition is TRUE or FALSE. If the condition is FALSE, it will again perform the same task. If the condition is TRUE, it will exit the Loop immediately.

Example

We know it is difficult to understand anything in the theory part, but there is nothing to worry about. We will give you easy examples to understand the Loop. Read on. To start the learning, let’s perform the task of inserting the first 10 serial numbers from cell A1 to A10.

Follow the below steps to apply the Do Until Loop.

Step 1: Create a macro name first to start the subprocedure.

Code:

Sub Do_Until_Example1()

End Sub
Example 1.1

Step 2: Define a variable as “Long.”  I have defined “x” as a long data type.

Dim x As Long
Example 1.2

Step 3: Now, enter the word “Do Until.”

Do Until
Example 1.3

Step 4: Enter the condition as “x =11” after starting the Loop name.

Do Until x = 11
Example 1.4

x = 11 is the logical test we have applied. So, this line says to run the loop until x equals 11.

Step 5: Apply CELLS property. Let us insert serial numbers from 1 to 10.

Cells(x, 1).Value = x
Example 1.5

Note: Here, we have mentioned “x” starts from 1, so at first x value is equal to 1. Wherever “x” is there is equal to 1.

Step 6: Close the Loop by entering the word “LOOP.”

Sub Do_Until_Example1()

Dim x As Long

Do Until x = 11
Cells(x, 1).Value = x
Loop

End Sub

vba do until example 1.6

We have completed the coding part. We will now test the codes to understand the Loop better.

To run the line-by-line code, first press the F8 key.

vba do until shortcut key

It will first highlight the macro name in yellow color.

vba do until example 1.7

When you see the yellow line, it says that that is not executed by about to execute if you press the F8 key one more time.

Now, press the F8 key one more time. Again, the yellow line will go to Do Until Loop.

vba do until example 1.8

Now, to understand the Loop, place a cursor on the variable “x” and see the value of variable “x.”

vba do until example 1.9

So, x=0. Since the highlighted line is the first line in the Loop, the value of “x” is zero. So, press the F8 key once again and see the value of “x.” Before that exit, the code runs and assigns the value to “x” as 1.

vba do until example 1.10

Now again, start running a loop by pressing the F8 key. See the value of “x.”

vba do until example 1.11

Now, the value of “x” shows as 1. So, to have incremental value to the variable “x,” we need to reassign the value of variable “x” as x = x + 1 inside the Loop.

vba do until example 1.12

Now, press the F8 key one more time. Again, we should get the value of 1 in cell A1.

vba do until example 1.13

Now, press the F8 key once again and see the value of “x.”

vba do until example 1.14

The value of variable “x” is 2 now. So, our condition says to run the Loop until the condition becomes TRUE, so our Loop keeps running until the value of “x” becomes 11.

Press F8 one more time. It will jump back to the Do Until Loop line.

example 1.15

Press the F8 key two more times. Finally, we will get the value of 2 in cell A2.

vba do until example 1.16

Press the F8 key again. The value of “x” becomes 3 now.

excel vba do until loop - example 1.17

Press the F8 key again. It will jump back to the Loop once again.

excel vba do until loop - example 1.15

Like this, this Loop will continue executing the task until the value of “x” becomes 11. So, now we have executed the Loop until the “x” value becomes 11.

excel vba do until loop - example 1.18

Now, if we press F8, it still will go back to the Loop.

excel vba do until loop - example 1.15

But if we press the F8 key now, it will exit the Loop because the applied condition becomes “TRUE,” i.e., x = 11.

end loop - example 1.19

So, we have serial numbers from 1 to 10 in the Excel sheet now.

Example 1.20

So, this is the basic idea of the Do Until Loop. To understand any Loops, you need to run the code line by line until you get the full knowledge about Loops.