×

Excel VBA: Do Until….Loop

DO UNTIL….Loop


The “Do Until” Loop is same unlike DO WHILE statement just that it will keep on looping till the condition is not met. This loop is used to repeat a set of statements if the condition is false. The condition may be checked at the beginning of the loop or at the end of Do Until loop.

Syntax

Do Until condition
    [statement 1]
    [statement 2]
    ...
    [statement n]
    [Exit Do] 
    [statement 1]
    [statement 2]
    ...
    [statement n]
 Loop 

Alternate Syntax

Do 
  [statement 1] 
  [statement 2] 
   ... 
 [statement n] 
  [Exit Do] 
   [statement 1] 
   [statement 2] 
   ... 
  [statement n] 
 Loop Until condition 

In the first syntax, the “Do Until” loop checks the condition first and returns the condition result as TRUE or FALSE. If the condition returns true, the loop gets terminated else for False t will execute the code and perform a specified task.

In the second syntax, it will execute the cod task then it test for the condition to check whether the condition is TRUE or FALSE. If the condition is FALSE it will perform the same task again. If the condition is TRUE then it will exit the loop. Even if the condition is TRUE, but it will still run at least once in the second syntax.

Example 1: Write a Macro, using Do Until Loop to print the odd numbers between 1 to 10.  
In the above example, if you change the condition of Do Until I = 10 instead of 11, since variable ‘I’ will never reach this value 10, as 1+2 = 3, then 3+2 = 5 and so on.. i.e. we will have only odd numbers, excel will keep on running till value of I is 32,767 which is the maximum value a variable declared as integer can take. This is how Do Until is different from Do While.

Sub DoUntil_Example1()
 Dim I As Integer
 I = 1
     'repeat a set of statements if the condition is false.
     Do Until I = 11
         Worksheets("Sheet2").Cells(I, 2).Value = I
         I = I + 2 
     Loop
 End Sub 

Output

1

3

5

7

9

Do Unti Loop in VBA

Example 2: Write a procedure or macro using Do Until that does the following task:

Get "Qualified" in column E if the value of column D is above 200 else it should return “Not Qualified”

Sub DoUntilLoop_Example2()
 Dim CurRow As Byte
 CurRow = 2
     'checks the condition first and get the condition result as TRUE or FALSE.
     Do Until Cells(CurRow, 1) = ""
         If Cells(CurRow, 4) > 200 Then
             Cells(CurRow, 5) = "Qualified"
         Else 
             Cells(CurRow, 5) = "Not Qualified"
         End If
         CurRow = CurRow + 1
     Loop    
 End Sub 

Output

Name Title Date Sale Amount Qualified/ Not Qualified
Itrat Zaidi IT Manager 6/8/2016 $220.00 Qualified
Varun Mahanty Sales 1/1/2014 $251.00 Qualified
Elmer C. Laslo Customer Service 6/16/2016 $217.00 Qualified
Sohrab Grait Data Entry Specialist 5/31/2015 $344.00 Qualified
Gerard M. Banmiller Customer Service 9/8/2014 $980.00 Qualified
Arvind Patra IT Manager 11/22/2015 $178.00 Not Qualified
John C. Black Customer Service 3/31/2014 $258.00 Qualified
N. William White Data Entry Specialist 12/21/2015 $38.00 Not Qualified
Robert F. Mangano Data Entry Specialist 7/16/2015 $371.00 Qualified
Rohit Thakkar IT Tech 12/21/2017 $67.00 Not Qualified
Thomas E. Vessey Customer Service 1/1/2017 $437.00 Qualified
procedure or macro using Do Until

Example 3: In the following procedure, the “i < 11” condition is tested at the beginning of the loop, Loop ends because the condition is met. Hence, the it will exit the loop.

Sub DoUntil_Example3()
 'Declaring the variable
 i = 6
 'It will check the condition and if it is true then will run the block of code
     Do Until i < 11
         i = i + 1
         MsgBox "The value of the variable is: " & i 
     Loop
 End Sub 

Output

Alternate Syntax

Example 4: In the following procedure, the “i < 11” condition is tested at the end of the loop. So firstly the code will be executed. Hence, the it will return 7. Then will check the condition and terminate the loop.

Sub DoUntil_Example4()
 'Declaring the variable
 i = 6
 'It will firstly run the program and then check the condition
     Do
         i = i + 1
         MsgBox "The value of the variable is: " & i
     Loop Until i < 11
 End Sub 

Output

DO UNTIL….Loop

Related Topics

Excel VBA Cos Function

VBA Cos Function: The Cos function in VBA returns the cosine value for a supplied angle. Syntax Cos (Number) Parameter Number (required) –This parameter represents the number that you want the absolute value of. Return This function...

1 minute read.

Excel VBA Date Function

VBA Date Function The Date function in VBA returns the current date. Syntax Date( ) Parameter NA Return This function returns the current date. Example 1 Sub DateFunction_Example1() ' retuning the current date in the variable currentDate Dim currentDate As...

1 minute read.

VBA ActiveSheet

What is VBA Activesheet Property? The active sheet means the current worksheet which you are working on and viewing. The ActiveSheet object signifies the worksheet tab that is selected before running the VBA...

5 minutes read.

Looping in VBA

Looping in VBA There are many situations where a programmer needs to execute a block of the repetitive code number of times. Writing the same statement will make the program tedious and monotonous....

3 minutes read.

CodeIgniter File Uploading Class

The Codeigniter provides a file Uploading library class which is used to upload any file such as images, pdf, mp3, etc. to the codeigniter’s application. It also allows to set various preferences such...

5 minutes read.

Excel VBA Split Function

Excel VBA Split Function: The Split function in VBA is used to split a string into several substrings and return a one-dimensional array of substrings. Syntax Split (Expression, [Delimiter], [Limit], [Compare]) Parameter Expression (required) – This parameter...

2 minutes read.

Excel VBA Tutorial

What is VBA? Introduction to Excel VBA: Visual Basic for Applications (VBA) is a programming language developed by Microsoft to automate operations in applications, such as Excel, Word, PowerPoint, etc. It...

5 minutes read.

Excel VBA Filter Function

VBA Filter Function: The Filter function in VBA returns a subset for the given string array, based on specified criteria. Syntax Filter (SourceArray, Match, [Include], [Compare]) Parameter SourceArray (required) – This parameter the array of Strings that you...

2 minutes read.

Excel VBA Month Function

The Mont function in VBA returns the month number for the specified date. Syntax Month (Date) Parameter Time (required) – This parameter represents the date. Return This function returns the month number for the specified date. Example 1 Sub MonthFunction_Example1() ...

1 minute read.

Excel VBA Array

Introduction to VBA Array An array is a type of variable that holds more than one piece of data. In VBA, you can refer to a specific variable (element) of an array by using...

5 minutes read.

For Next loop in VBA

For Next Loop The ”For Next” loop is used for a fixed number of times. It works by implementing the loop for the specified number of times. In this, the user specifies how...

3 minutes read.

Excel VBA DateSerial Function

The DateSerial function in VBA returns a Date from a supplied year, month, and day number. Syntax DateSerial (Year, Month, Day) Parameter Year (required) – This parameter represents an integer signifying the year. Month (required) – This parameter...

2 minutes read.

Excel VBA GoTo Statement

GoTo Statement he GoTo statement branches unconditionally to a specified line in a procedure. It is used to transfer the program control to a new statement, which is headed by a label. It sends...

2 minutes read.

Excel VBA IsNumeric Function

VBA IsNumeric Function: The IsNumeric function in VBA returns a Boolean value showing whether the specified Expression contains a numeric value or not. Syntax IsNumeric (Expression) Parameter Expression (required)- This parameter represents the variant that...

1 minute read.

VBA ActiveCell Property

What is the ActiveCell Property? The active cell signifies the active selected cell in the current worksheet. The Active property acts as a reference point and is used to move the cell cursor...

5 minutes read.

VBA TimeSerial Function

The TimeSerial function in VBA returns a Time for the specified hour, minute, and second. Syntax TimeSerial (Hour, Minute, Second) Parameter Hour (required) – This parameter represents an integer (0 to 23), signifying the hour of the time. Minute...

2 minutes read.

Excel VBA Log Function

VBA Log Function: The Log function in VBA returns the natural logarithm for the specified number. Syntax Log (Number) Parameter Number (required) –This parameter represents a positive numeric value that you want to find the...

1 minute read.

Excel VBA Exp Function

VBA Exp Function: The Exp function in VBA returns the value of the exponential function ex (mathematical constant ‘e’ raised to specified power) for the given value of x. Syntax Exp (Number) Parameter Number (required) –This parameter represents...

1 minute read.

Excel VBA String Function

The String function in VBA creates a String, consisting of several repeated characters. Syntax String (Number, Character) Parameter Number (required) – This parameter represents the number of characters in the returned String. Character (required) – This parameter...

1 minute read.

Excel VBA InStr Function

VBA InStr Function The InStr function in VBA searches for a substring inside the given string. It returns the position of a substring within a string, as an integer if the substring is found...

1 minute read.