Lesson 37 of 60 – Do While Loop in VBA
62%

Do While Loop in VBA

The Do While loop in VBA is used to repeat a block of code as long as a specified condition is True.

Unlike the For...Next loop, which normally uses a fixed starting and ending value, a Do While loop continues based on a condition.

For example, you can use a Do While loop to process student records while a condition remains true.

Note: A Do While loop is useful when the number of repetitions is not necessarily known in advance and the loop should continue while a condition remains true.

1. What is a Do While Loop?

A Do While loop repeats VBA statements while a specified condition is True.

Basic structure:

Do While condition

    statements

Loop

The condition is checked before each iteration.

2. Why Use Do While?

Do While is useful when you want code to continue running as long as a condition is satisfied.

Common uses include:

  • Processing records until a condition changes.
  • Reading data while cells contain values.
  • Repeating calculations.
  • Processing student records.
  • Searching through worksheet data.
  • Repeating a task until a limit is reached.

3. Basic Do While Syntax

The basic syntax is:

Do While condition

    statements

Loop

VBA checks the condition. If it is True, the statements execute. The condition is checked again when VBA reaches Loop.

4. Simple Do While Example

The following example displays numbers from 1 to 5.

Dim i As Integer

i = 1

Do While i <= 5

    MsgBox i

    i = i + 1

Loop

The loop continues while i <= 5.

5. Understanding the Condition

The condition determines whether the loop should continue.

Do While i <= 5

When the condition is True, the loop executes. When it becomes False, the loop stops.

6. Initializing the Variable

Before starting a Do While loop, the variable used in the condition should normally have an appropriate initial value.

Dim i As Integer

i = 1

Do While i <= 5
    MsgBox i
    i = i + 1
Loop

Here, i starts with the value 1.

7. Updating the Loop Variable

The loop variable must usually be changed inside the loop so that the condition can eventually become False.

i = i + 1

Without updating the variable in an appropriate way, the loop may continue indefinitely.

8. Do While with Excel Cells

A Do While loop can write values into Excel cells.

Dim i As Integer

i = 1

Do While i <= 5

    Cells(i, 1).Value = i

    i = i + 1

Loop

This writes numbers 1 to 5 into cells A1 through A5.

9. Do While with Text

You can repeatedly write text into cells.

Dim i As Integer

i = 1

Do While i <= 5

    Cells(i, 1).Value = "Student"

    i = i + 1

Loop

The word Student is written into five cells.

10. Do While with Calculations

The loop can perform a calculation during every iteration.

Dim i As Integer

i = 1

Do While i <= 10

    Cells(i, 1).Value = i * 10

    i = i + 1

Loop

This writes 10, 20, 30, and so on up to 100.

11. Do While with If Statement

An If statement can be placed inside a Do While loop.

Dim i As Integer

i = 2

Do While i <= 10

    If Cells(i, 2).Value >= 40 Then
        Cells(i, 3).Value = "Pass"
    Else
        Cells(i, 3).Value = "Fail"
    End If

    i = i + 1

Loop

This checks student marks row by row.

12. Do While with MsgBox

MsgBox can be used inside a Do While loop.

Dim i As Integer

i = 1

Do While i <= 3

    MsgBox "Hello " & i

    i = i + 1

Loop

Three message boxes will be displayed.

13. Do While and Counter

A counter is often used to control the loop.

Dim count As Integer

count = 1

Do While count <= 10

    MsgBox count

    count = count + 1

Loop

The counter increases by one during each iteration.

14. Do While with a Range

You can process a worksheet range using a row counter.

Dim rowNumber As Integer

rowNumber = 2

Do While rowNumber <= 10

    Cells(rowNumber, 1).Font.Bold = True

    rowNumber = rowNumber + 1

Loop

Rows 2 through 10 are processed.

15. Do While for Student Records

Do While can process student records one row at a time.

Dim rowNumber As Integer

rowNumber = 2

Do While rowNumber <= 11

    Cells(rowNumber, 4).Value = "Active"

    rowNumber = rowNumber + 1

Loop

The status of rows 2 through 11 is set to Active.

16. Do While Until an Empty Cell

A Do While loop can continue while a cell contains data.

Dim rowNumber As Integer

rowNumber = 2

Do While Cells(rowNumber, 1).Value <> ""

    MsgBox Cells(rowNumber, 1).Value

    rowNumber = rowNumber + 1

Loop

The loop continues while column A contains a value.

17. Do While for Searching

A Do While loop can be used to search through worksheet data.

Dim rowNumber As Integer

rowNumber = 2

Do While Cells(rowNumber, 1).Value <> ""

    If Cells(rowNumber, 1).Value = "Rahul" Then
        MsgBox "Rahul Found"
    End If

    rowNumber = rowNumber + 1

Loop

18. Do While for Total Calculation

A loop can calculate the total of values stored in a worksheet.

Dim rowNumber As Integer
Dim total As Double

rowNumber = 2
total = 0

Do While rowNumber <= 6

    total = total + Cells(rowNumber, 2).Value

    rowNumber = rowNumber + 1

Loop

MsgBox "Total = " & total

19. Do While with Multiple Statements

A Do While loop can contain several statements.

Dim i As Integer

i = 1

Do While i <= 5

    Cells(i, 1).Value = i
    Cells(i, 2).Value = i * 10
    Cells(i, 3).Value = i * 100

    i = i + 1

Loop

Each iteration performs three operations.

20. Do While for Multiplication Table

You can create a multiplication table using a Do While loop.

Dim i As Integer
Dim number As Integer

number = 5
i = 1

Do While i <= 10

    Cells(i, 1).Value = number & " x " & i
    Cells(i, 2).Value = number * i

    i = i + 1

Loop

21. Do While with InputBox

InputBox can be used inside a loop, although the loop must have a clear stopping condition.

Dim number As Integer
Dim count As Integer

count = 1

Do While count <= 3

    number = CInt(InputBox("Enter a number:"))

    MsgBox "You entered " & number

    count = count + 1

Loop

The user is asked for a number three times.

22. Do While with Boolean Condition

A Boolean variable can control a Do While loop.

Dim continueLoop As Boolean
Dim i As Integer

continueLoop = True
i = 1

Do While continueLoop

    MsgBox i

    i = i + 1

    If i > 3 Then
        continueLoop = False
    End If

Loop

The loop stops when the Boolean variable becomes False.

23. Do While with Comparison Operators

Different comparison operators can be used in the condition.

Dim i As Integer

i = 10

Do While i > 0

    MsgBox i

    i = i - 1

Loop

The loop continues while i > 0.

24. Do While and Excel Data

Do While is particularly useful when processing a list until an empty cell is encountered.

Dim rowNumber As Integer

rowNumber = 2

Do While Cells(rowNumber, 1).Value <> ""

    Cells(rowNumber, 2).Value = "Processed"

    rowNumber = rowNumber + 1

Loop

The loop continues until an empty cell is found in column A.

25. Practical Student Result Example

The following example checks student marks until an empty student name is found.

Dim rowNumber As Integer

rowNumber = 2

Do While Cells(rowNumber, 1).Value <> ""

    If Cells(rowNumber, 2).Value >= 40 Then

        Cells(rowNumber, 3).Value = "Pass"

    Else

        Cells(rowNumber, 3).Value = "Fail"

    End If

    rowNumber = rowNumber + 1

Loop

Column A contains student names, column B contains marks, and column C receives the result.

26. Common Mistakes with Do While

Beginners commonly make these mistakes:

  • Forgetting to initialize the loop variable.
  • Forgetting to update the variable.
  • Creating a condition that never becomes False.
  • Using the wrong cell or row reference.
  • Forgetting the Loop statement.
  • Processing more rows than intended.

A loop that never makes its condition False can continue indefinitely.

27. Practical Data Processing Example

The following example processes records until an empty cell is found.

Dim rowNumber As Integer

rowNumber = 2

Do While Cells(rowNumber, 1).Value <> ""

    Cells(rowNumber, 4).Value = "Processed"
    rowNumber = rowNumber + 1

Loop

MsgBox "Processing Completed"

This is useful for simple Excel data-processing tasks.

28. Best Practices for Do While

  • Always make the loop condition clear.
  • Initialize variables before starting the loop.
  • Make sure the condition can eventually become False.
  • Update the controlling variable when required.
  • Test the loop with a small amount of data first.
  • Use meaningful variable names.
  • Be careful when processing large worksheets.

29. Complete Do While Workflow

A typical Do While workflow is:

  1. Declare the controlling variable.
  2. Give it an initial value.
  3. Write the Do While condition.
  4. Execute the required statements.
  5. Update the controlling variable.
  6. Reach the Loop statement.
  7. Check the condition again.
  8. Stop when the condition becomes False.
Dim i As Integer

i = 1

Do While i <= 5

    Cells(i, 1).Value = i

    i = i + 1

Loop

30. Complete Understanding of Do While

The Do While loop is an important VBA looping structure that repeats code while a specified condition remains True.

It is particularly useful when processing Excel data until a condition changes, such as processing rows until an empty cell is found.

Dim rowNumber As Integer

rowNumber = 2

Do While Cells(rowNumber, 1).Value <> ""

    If Cells(rowNumber, 2).Value >= 40 Then

        Cells(rowNumber, 3).Value = "Pass"

    Else

        Cells(rowNumber, 3).Value = "Fail"

    End If

    rowNumber = rowNumber + 1

Loop

In this example, VBA continues processing student records while column A contains a student name. The marks are checked and the result is written into column C.

The next lesson will introduce the Do Until loop, which repeats code until a specified condition becomes True.

📌 Key Points

  • Do While repeats code while a condition is True.
  • The condition is checked before each iteration.
  • The loop uses the Loop keyword to return to the condition.
  • A controlling variable often needs to be updated inside the loop.
  • Do While can process Excel cells and rows.
  • Do While can work with If statements and calculations.
  • It can continue processing data until an empty cell is found.
  • Always make sure the loop can eventually stop.
  • Do While is useful when repetition depends on a condition.

🧠 Quick Quiz

Question: When does a Do While loop continue executing its code?