×

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

VBA Creating, Displaying, Uploading UserForms

UserForm is a customized interface and acts as a VBA container and can add various controls as per the required functionality, each of which has certain usage and related properties. You can...

4 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 RTrim Function

VBA RTrim Function: The Rtrim function in VBA removes the leading spaces from the text in the specified string. Syntax RTrim (String) Parameter String (required) – This parameter represents the string from which you want...

1 minute read.

Excel VBA Sin Function

VBA Sin Function: The Sin function in VBA returns the sine value for a supplied angle. Syntax Sin (Number) Parameter Number (required) –This parameter represents the angle (in radians) to calculate the sine value. Return This function...

1 minute read.

How to save Macro Workbook?

Saving Macro Workbook Default Excel File Extension The default excel file extension is “.xlsx”. But the standard file extension “.xlsx” cannot contains macros. So, the workbook contains macro when save din.xlsx file, all VBA...

1 minute read.

Excel VBA TimeValue Function

Excel VBA TimeValue Function: The TimeValue function in VBA returns a Time from the specified String interpretation of a time /date where the date information for the given string is...

1 minute read.

Excel Objects in VBA

What are Excel Objects? The Excel objects belong to the entities that make up an Excel Workbook, Worksheets, Columns, Rows, Cell Ranges, etc. Each object in Excel has loads of Properties that are...

6 minutes read.

Excel VBA Error Handling

What is Errors and Types of Error? Errors are conditions that resist the flow of the program or enables a problem while running any programming. There are three types of errors in VBA...

5 minutes read.

Userform Events

What are Events? Anything you do in to trigger an excel file is an event (an action). Example: If you want a greeting message ‘Good Day’ whenever an excel file is...

7 minutes read.

Miscellaneous Exercise of conditional statements and Loop

We have the already worked with syntax and examples of conditional statements and loops in the previous tutorials. In this tutorial, we will learn how to work with both together.  We will explain...

3 minutes read.

Basics of Userform

A User Form is a built-in customized dialog box that fetches data from the user through a user-friendly dialog box or window that makes up part of an application's user interface. It...

9 minutes 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.

Introduction to Visual Basic Editor Window

How to enable the Developer Ribbon Tab? In order to work with VBA, users need to make a small change in Excel to display a new tab (Developer) at the top of the...

5 minutes read.

Excel VBA Join Function

Excel VBA Join Function: The Join function in VBA is used to join an array of substrings and return them all as a single string. Syntax Join (SourceArray, [Delimiter]) Parameter SourceArray (required) – This parameter...

1 minute read.

Excel VBA: DO WHILE….Loop

DO WHILE….Loop The “Do While” Loop is the same, unlike the FOR statement, just that it will keep on looping till the specified condition is true. It is used when we want to...

3 minutes read.

Excel VBA Sgn Function

VBA Sgn Function: The Sgn function in VBA returns an integer (+1, 0, or -1), stating the arithmetic sign for the specified number. Syntax Sgn (Number) Parameter Number (required) –This parameter represents the number that...

1 minute read.

Excel VBA LBound Function

Excel VBA LBound Function: The LBound function in VBA returns the lowest subscript for the specified dimension in the given array. Syntax LBound (ArrayName, [Dimension]) Parameter ArrayName (required) – This parameter represents an array for which...

1 minute read.

Excel VBA LCase Function

VBA LCase Function: The LCase function in VBA converts the given String into lower case text. Syntax LCase (String) Parameter String (required)- This parameter represents the text string that you want to convert to...

1 minute read.

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...

4 minutes read.

VBA Charts Basic Operations

VBA Charts- Basic Operations The Chart Object in Excel VBA represents the collection of all the charts sheet present in a workbook. A chart can be either an embedded chart or a separate chart sheet. The...

6 minutes read.