Do while loop in excel VBA

  • Do While loop in excel VBA is used when we want to repeat a set of statements as long as the condition is true. The condition may be checked at the beginning of the loop.
  • Repeats a block of statements while a condition is True or until a condition becomes True.

Syntax

Do While condition
statement 1
statement 2
statement n
Loop

Do the following examples

1) In Column A print/show number from 1 to 10 using do while loop

Sub Dowhile()
i = 0
Do While i < 10
i = i + 1
\’ MsgBox \”The value of i is : \” & i
Cells(i, 1).Value = i
Loop
End Sub


2) In Column B get table of number input by user using do while loop


Sub Dowhile2()
Dim i As Integer
Dim n As Integer
n = InputBox(\”type a number \”)

i = 0
Do While i < 10
i = i + 1
\’ MsgBox \”The value of i is : \” & i
Cells(i, 2).Value = n * Cells(i, 1).Value
Loop
End Sub