×

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 repeat a set of statements where the condition may be checked at the beginning or at the end of the loop.

Syntax

Do While condition
    [statement 1]
    [statement 2]
    ...
    [statement n]
 Loop    

Alternate Syntax

Do 
    [statement 1]
    [statement 2]
    ...
    [statement n]
 Loop While (condition) 

The difference between both the syntax is that in the first one, the While condition is checked first before any code block or statement is executed, and in the second syntax, the given statement or code block is executed first, and then the While condition is checked. Even if the while condition is false, but it will still run at least once in the second syntax.

Example 1: Write a macro using Do While loop to print the odd number between 1 -10.

Sub DoWhile_Example1()
 Dim val As Integer
 val = 1
     'Do While will loop until the cells' value is not equal to empty
     Do While val <= 10
         'with the help of worksheet property inserting the values
         Worksheets("Sheet2").Cells(val, 1).Value = val 
         val = val + 2
     'Closing the Do While loop
     Loop
 End Sub 

Output

1

3

5

7

9

DO WHILE….Loop VBA

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

Get "Qualified" in column E if the value of column D is above 200

Sub DoWhile_Loop_Example1()
 'initializing the variable r
 Dim r As Byte
 'Setting up a value of variable 'r'
 r = 2
     'Do While will loop until the cells' value is not equal to empty
     Do While Cells(r, 1) <> ""
         'IF Condition Block
         If Cells(r, 4) > 200 Then 
             Cells(r, 5) = "Qualified"
         Else
             Cells(r, 5) = "Disqualified"
         End If
         'Adding one to r counter to move to the next cell
         r = r + 1
     'Closing the while loop 
     Loop
 End Sub 

Output

Name Title Date Sale Amount Qualified/Disqualified
Leo Hendry Customer Service 6/8/2016 $220.00 Qualified
Rani Bhardwaj Sales 1/1/2014 $251.00 Qualified
Elmer C. Laslo Customer Service 6/16/2016 $217.00 Qualified
Ayush Khurana Data Entry Specialist 5/31/2015 $344.00 Qualified
Gerard M. Banmiller Customer Service 1/11/2014 $980.00 Qualified
Gregory L. Gibson IT Manager 11/22/2015 $178.00 Disqualified
Megha Malik Customer Service 3/31/2014 $258.00 Qualified
N. William White Sales Manager 12/21/2015 $38.00 Disqualified
Robert F. Mangano Data Entry Specialist 7/16/2015 $371.00 Qualified
Itrat Zaidi IT Tech 6/8/2016 $67.00 Disqualified
Thomas E. Vessey Business Development Executive 3/21/2016 $437.00 Qualified
DO WHILE….Loop in VBA2

Example 3: In the following procedure, the “n>6” condition is tested at the beginning of the loop, Loop ends because the condition is not met. Hence, the msgbox returns the value of 0.

Sub DoWhile_Example3()
 Dim n As Integer
 Dim nTotal As Integer
 n = 6
 nTotal = 0
     'While condition is checked at beginning of the code
     Do While n > 6
         'the statement won't get executed 
         nTotal = n + nTotal
         n = n - 1
     Loop
 'it will return 0
 MsgBox nTotal 
 End Sub 

Output

DO WHILE….Loop

While Using the Second Syntax

Example 4: In the following procedure, the “n>6” condition is tested at the end of the loop, and because it is met, the loop executes, but only once after which the value of n decreases to 5, and the Loop ends. The msgbox returns the value of 6.

Sub DoWhile_Example4()
 Dim n As Integer
 Dim nTotal As Integer
 n = 6
 nTotal = 0    
     Do
         nTotal = n + nTotal
         n = n - 1 
     'While condition is checked after the code has been executed once
     Loop While n > 6
 MsgBox nTotal
 End Sub 

Output

DO WHILE….Loop in VBA 4

Related Topics

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 Rnd Function

VBA Rnd Function: The Rnd function in VBA returns a random number that is greater than or equal to (>=) 0 and is less than (<) 1. Syntax Rnd ([Number]) Parameter Number (optional) –This...

1 minute read.

VBA Cell

What is VBA cell? Cells is one of the elements (workbook, worksheet, range) in Excel VBA, which refers to cells of the Excel worksheet.  In VBA, the cell is also a property...

5 minutes read.

VBA Option Explicit

VBA Option Explicit The Option Explicit in VBA is used to declare the variables at the top of your macro code. It is the most secure and easy option to maintain your variables....

5 minutes read.

Excel VBA DateValue Function

The DateValue function in VBA returns a VBA Date from the given String representation of a date wherein the time information is ignored. It is unable to interpret dates that include the...

1 minute read.

Steps to Create a Chart in VBA

Steps to Create a Chart Charts are created either by directly working with the chart variable object that defines the chart data or by ChartObject method. In order to get to...

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

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 Fix Function

VBA Fix Function: The Fix function in VBA truncates the given number to an integer and returns the rounded off integer number. This function both positive and negative numbers to zero. Syntax Fix...

1 minute read.

Excel VBA CStr Function

VBA CStr Function: The CStr function in VBA converts an expression into a string data type. Syntax CStr (Expression) Parameter Expression (required) – This parameter represents the expression that you want to convert to a...

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 Not Equal Operator

What is VBA Not Equal Operator? VBA Not Equal binary operator (“<>”) is a logical function that is used to check if the specified values are not equal or not. This...

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 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 IsDate Function

VBA IsDate Function: The IsDate function in VBA returns a Boolean value indicating whether the given expression is interpreted as a VBA Date or not. Syntax IsDate (Expression) Parameter Expression (required) – This parameter...

1 minute read.

Excel VBA StrConv Function

The StrConv function in VBA converts a string into a specified format. Syntax StrConv (String, Conversion, [LocaleID]) Parameter String (required) – This parameter represents the string to be converted. Conversion (required) – This parameter specifies the type of conversion. It can...

1 minute read.

Excel VBA Val Function

VBA Val Function: The Val function in VBA converts the given string into a numeric value. This function ignores spaces and continues to read the characters after space(s). It stops...

1 minute read.

Debugging in Excel VBA

Debugging in VBA: Debugging is a technique used to fix errors in programming languages. In Excel VBA, we have different ways by which you can identify the error in the...

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

Excel VBA Sqr Function

VBA Sqr Function: The Sqr function in VBA returns the square root for the specified number. Syntax Sqr (Number) Parameter Number (required) – This parameter represents a positive numeric value that you want to calculate...

1 minute read.