VBA Do While loop in Excel

Do While loop is similar to For loop except that the increment is defined within the loop.

The script within the loop executes as long as the condition specified in the Do While statement is true.

This loop always end with "Loop" keyword.

Syntax

  1. Do While
  2. /code for what should be done here
  3. Loop

Example: The following code executes as long as Integer variable "I" is less than 5.

  1. Sub While_demo()
  2. Dim i As Integer
  3. Dim j(5) As Integer
  4. Do While i <= 5
  5. j(i) = i
  6. MsgBox j(i)
  7. i = i + 1
  8. Loop
  9. End Sub

A glance at the editorial screenshot is shown below.

excel vba do while loop

Result:

excel vba do while loop

excel vba do while loop

 

You can find similar Excel Questions and Answer hereunder

1) How to do loops in VBA with the for next loop

2) How can I find the last used cell in a Column in VBA?

3) How can I list all files in a folder using VBA?

4) Here some more advanced range objects and what you can do with the range object in VBA

5) How to go through each item of an array, table, sheet with the for each function

6) How can I find the count of records that meet a given condition in my raw data table?

7) How can I filter a table to get all records that have less than a particular value in a specific column?

8) How can I loop through all ActiveX checkboxes in WorkSheet and set them to Unchecked status?

9) How to do a nested loop in VBA or a loop in a loop

10) How to do webscrapping in VBA. Here some basics

 

Here the previous and next chapter