EDUCBA

EDUCBA

MENUMENU
  • Blog
  • Free Courses
  • All Courses
  • All in One Bundle
  • Login
Home VBA VBA Resources VBA Logical Functions VBA Do While Loop

VBA Do While Loop

Madhuri Thakur
Article byMadhuri Thakur

Updated April 8, 2023

VBA Do While Loop

Excel VBA Do While Loop

Do While Loop means to do something while the condition is TRUE. It is like a logical function which works based on TRUE or FALSE. So if the condition is TRUE, it will keep executing the statement inside the loop, but if the condition is FALSE straight away, it will exit the Do While statement. The working of the VBA Do While Loop is shown in the figure given below.

Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Mobile Apps, Web Development & many more.

VBA What is Do While Loop

Syntax of Do While Loop in VBA Excel

Do While Loop has two kinds of syntax in Excel VBA.

VBA Do While Syntax 1

VBA Do While Syntax 2

Both look very similar, and there is one simple differentiation in them.

In the first syntax “Do While” loop checks the condition first and gives the condition result is TRUE or FALSE. If the condition is TRUE, it will execute the code and perform a specified task, and if the condition is FALSE, then it will exit the loop.

In the second syntax “Do” loop firstly will execute the code, and then it tests whether the condition is TRUE or FALSE. If the condition is TRUE, it will again go back and perform the same task. If the condition is FALSE, then it will straight away exit the loop.

Example of Excel VBA Do While Loop

If you have not understood anything in the theory part nothing to worry about. I will explain to you the simple set of examples to have a fair knowledge about this loop.

You can download this VBA Do While Loop Excel Template here – VBA Do While Loop Excel Template

Now we will perform the task of inserting the first 10 serial numbers from cell A1 to A10. Follow the below steps to apply the “Do While” loop.

Step 1:

Create a macro name first.

Code:

Sub Do_While_Loop_Example1()

End Sub

VBA Do While Example 1

Step 2:

Define a variable as “Long”. I have defined “k” as a long data type.

Code:

Sub Do_While_Loop_Example1()

     Dim k As Long

End Sub

VBA Do While Example 1-2

Step 3:

Now enter the word “Do While”. And after starting the loop name, enter the condition as “k <=10”.

Code:

Sub Do_While_Loop_Example1()

     Dim k As Long

     Do While k <= 10

End Sub

VBA Do While Example 1-3

Step 4:

Now using the CELLS property, let’s insert serial numbers.

Code:

Sub Do_While_Loop_Example1()

  Dim k As Long

  Do While k <= 10
     Cells(k, 1).Value = k
End Sub

VBA Do While Example 1-4

Note: Here, variable “k” starts from 1, so at first k value is equal to 1. Wherever “k” is, there is equal to 1.

Step 5:

Now close the loop by entering the word “LOOP”.

Code:

Sub Do_While_Loop_Example1()

  Dim k As Long

  Do While k <= 10
     Cells(k, 1).Value = k
  Loop

End Sub

VBA Do While Example 1-5

Ok, we are done. For better understanding, let’s test this code one by one by pressing the F8 key once.

The first press F8 key, it will highlight the macro name by yellow color.

Example 1-6

Now one more time, press the F8 key; it will jump to Do While Loop. Place the cursor on “k” and see what the value is.

Example 1-7

So, K=0. The reason why “k” is equal to zero now because the loop is not started to executed here. Press F8 one more time and see what the value is.

Example 1-8

Still, the “k” value is zero. Ok, let’s do one thing now, stop the running of macro and assign the value of “k” as 1 before the loop starts.

Example 1-9

Now inside the loop, reassign the k value as k = k +1.

Example 1-10

Now start the process of executing the code line by line by pressing the F8 key. Start pressing the F8 and see what the value is when it executes the “Do While” loop.

Code:

Sub Do_While_Loop_Example1()

        Dim k As Long

k=1

        Do While k <= 10
Cells(k, 1).Value = k
k = k + 1

         Loop

End Sub

Example 1-11

So the “k” value is equal to 1. Wherever “k” is, there is equal to the value of 1. So the line of code Cells (k, 1). Value = k is equal to:

Cells (1, 1).Value = 1 i.e. Row 1 & Column 1 (A1 cell) value is equal to 1.

Press the F8 key and execute the action inside the loop.

Example 1-12

Now, look at the value in cell A1.

Example 1-13

So we got the value of 1 in cell A1.

Now execute the next line by pressing the F8 key and see what the value of “k” is.

Example 1-14

So now the k value is equal to 2. So wherever k is, there is equal to 2.

Press the F8 key; now, the code will return to test the value of k.

Example 1-15

Now press two more times the F8 key and see what the value is in cell A2.

Example 1-16

Like this, Do While Loop keeps executing the task of inserting serial numbers until the value of k reaches 11. Now I have already executed line by line until the k value becomes 10.

Example 1-17

Now, if I press one more time F8 key, it will go back to test the condition but will not execute the line of code because the k value is more than 10 now.

Example 1-18

If I press the F8 key, it will straight away exit the loop and goes to End Sub.

Example 1-19

The final output is given as follows.

Example 1-20

Like this, VBA Do While Loop can be used to perform the same set of tasks until the given condition is TRUE.

Things to Remember

  • VBA Do While Loop executes the task until the condition is TRUE.
  • We can also test the condition at the beginning of the loop or also at the end of the loop.
  • The movement condition is FALSE; it will exit the loop and will not perform the task.

Recommended Articles

This has been a guide to VBA Do While Loop. Here we discussed how to use Excel VBA Do While Loop along with some practical examples and a downloadable excel template. You can also go through our other suggested articles –

  1. VBA Function
  2. VBA XML
  3. VBA VLOOKUP
  4. VBA IsError
All in One Excel VBA Bundle
500+ Hours of HD Videos
15 Learning Paths
120+ Courses
Verifiable Certificate of Completion
Lifetime Access
Financial Analyst Masters Training Program
1000+ Hours of HD Videos
43 Learning Paths
250+ Courses
Verifiable Certificate of Completion
Lifetime Access
All in One Data Science Bundle
1500+ Hour of HD Videos
80 Learning Paths
360+ Courses
Verifiable Certificate of Completion
Lifetime Access
All in One Software Development Bundle
3000+ Hours of HD Videos
149 Learning Paths
600+ Courses
Verifiable Certificate of Completion
Lifetime Access
Primary Sidebar
Financial Analyst Masters Training Program1000+ Hours of HD Videos | 43 Learning Paths | 250+ Courses | Verifiable Certificate of Completion | Lifetime Access
All in One Data Science Bundle1500+ Hour of HD Videos | 80 Learning Paths | 360+ Courses | Verifiable Certificate of Completion | Lifetime Access
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign In
  • Live Classes
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Excel Charts
  • Excel Tips
  • All Tutorials
Certification Courses
  • All Courses
  • Excel VBA Course - All in One Bundle
  • VBA Course
  • Excel Data Analysis Course
  • Excel for Marketing Course
  • Excel for Finance Course
  • Excel for HR Training

ISO 10004:2018 & ISO 9001:2015 Certified

© 2023 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

EDUCBA
Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Mobile Apps, Web Development & many more.

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more