×

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

Excel VBA FormatPercent Function

VBA FormatPercent Function: The FormatPercent function in VBA is used to apply a percent format to a numeric expression, and it returns the result as a string. Syntax FormatPercent (Expression, [NumDigitsAfterDecimal], [IncludeLeadingDigit], [UseParensForNegativeNumbers], [GroupDigits]) Parameter Expression (required)...

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

Excel VBA ABS Function

VBA ABS Function: The ABS function in VBA returns the absolute value of the specified number. Syntax Abs (Number) Parameter Number (required) – This parameter represents the number that you want the absolute value of. Return This...

1 minute read.

Excel VBA CCur Function

The CCur function in VBA is used to convert an expression into a Currency data type. It can take a maximum of 15 digits to the left of the decimal place and...

1 minute read.

Excel VBA Array Function

VBA Array Function: The Array function in VBA generates an array containing the given set of values. Syntax Array (Arglist) Parameter Arglist (required) – This parameter the list of values that you want to make...

2 minutes read.

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

VBA StrComp Function: The StrComp function in VBA compares two strings and returns an integer value displaying the result of the comparison. Syntax StrComp (String1, String2, [Compare]) Parameter String1 (required)- This parameter represents the first string to...

2 minutes 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 Day Function

The function Day in VBA returns the day number (from 1 to 31) for the given date value. Syntax Day (Date) Parameter Date (required) – This parameter represents the date. Return This function returns the day...

1 minute 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 Updating Pivot Table

VBA- Updating Pivot Table The pivot table is an important feature to explore, summarize, and interpret the bulk amount of data. It helps data evaluating, reviewing, as well as making useful...

3 minutes read.

Excel VBA FormatNumber Function

VBA FormatNumber Function: The FormatNumber function in VBA is used to apply a number format to a numeric expression, and it returns the result as a string. Syntax FormatNumber (Expression, [NumDigitsAfterDecimal], [IncludeLeadingDigit], [UseParensForNegativeNumbers], [GroupDigits]) Parameter Expression (required)...

2 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: IF THEN Statement

VBA Excel: IF THEN StatementThis conditional statement enables you to check one condition and on the basis of that then run one or multiple statements if the condition holds. If...

1 minute read.

CodeIgniter Architecture

Here we will understand the architecture and working of the CodeIgniter application, which helps you to elaborate all steps in simple ways. As the above image represents that whenever a request comes from the...

2 minutes read.

Scope in Visual Basics

Definition of Scope The scope of any programming language implies the area of code where the variables will be identified, accessed, and used. Every variable has a scope associated with it. The scope of...

4 minutes read.

VBA Screen Updating

What is VBA Screen Updating property? Screen Updating is a VBA property which is used to display the output generation while running the code. If this property is enabled, we could see...

6 minutes read.