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.
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.
Do While is useful when you want code to continue running as long as a condition is satisfied.
Common uses include:
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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
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
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.
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
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.
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.
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.
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.
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.
Beginners commonly make these mistakes:
A loop that never makes its condition False can continue indefinitely.
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.
A typical Do While workflow is:
Dim i As Integer
i = 1
Do While i <= 5
Cells(i, 1).Value = i
i = i + 1
Loop
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.
Question: When does a Do While loop continue executing its code?