VBA Do Until Loop
Last Updated :
-
Blog Author :
Edited by :
Reviewed by :
Table Of Contents
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.
It is the opposite of the Do While Loop. It runs the Loops as long as the condition is TRUE.
Table of contents
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
Step 2: Define a variable as “Long.” I have defined “x” as a long data type.
Dim x As Long
Step 3: Now, enter the word “Do Until.”
Do Until
Step 4: Enter the condition as “x =11” after starting the Loop name.
Do Until x = 11
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
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
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.
It will first highlight the macro name in yellow color.
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.
Now, to understand the Loop, place a cursor on the variable “x” and see the value of variable “x.”
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.
Now again, start running a loop by pressing the F8 key. See the value of “x.”
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.
Now, press the F8 key one more time. Again, we should get the value of 1 in cell A1.
Now, press the F8 key once again and see the value of “x.”
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.
Press the F8 key two more times. Finally, we will get the value of 2 in cell A2.
Press the F8 key again. The value of “x” becomes 3 now.
Press the F8 key again. It will jump back to the Loop once again.
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.
Now, if we press F8, it still will go back to the Loop.
But if we press the F8 key now, it will exit the Loop because the applied condition becomes “TRUE,” i.e., x = 11.
So, we have serial numbers from 1 to 10 in the Excel sheet now.
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.
Recommended Articles
This article has been a step-by-step guide to VBA Do Until Loop. We discuss using Do Until Loop in Excel VBA and a practical example. Below are some useful Excel articles related to VBA: -