Loop without Do is a common error in Excel VBA Macro. Most of us often face this problem continuously. Predominantly this is a compile error and plays hide and sick most of the time. Thus, it kills both our time and productivity. Like many of us, if your intention is to get rid of VBA Loop without Do error, then yes! You are on the right track. We are going to show you 3 crucial reasons and solutions to fix this error.
Download Practice Workbook
To practice, please download the Excel Workbook file from the link below.
Introduction to Loop Without Do Error in Excel VBA
One of VBA’s most effective programming tools is the loop, which enables users to repeatedly run the same code block until a specific point is satisfied. The following portion of the code is executed by the program after the condition has been achieved.
Among three of these loops, while using Do Until and Do While loops in Excel VBA, we often get Do without Loop errors like the image below.
VBA Loop Without Do Error: 3 Possible Reasons and Solutions
Do Until and Do While loop is generally used to eliminate repetitive tasks manually. Nevertheless, during Do loop use, we use additional loops or conditionals such as IF, and Select Case. In some cases we face Compile error: Do without Loop. In this article, we illustrate 3 different reasons for Loop without Do error.
Reason 1. “Do” Statement is Missing in Your Excel VBA Code
We wrote a VBA code to obtain the first 10 Pythagorean Numbers where the sum of two squares is equal to the square of another number. In contrast, during the execution of code, we obtain Compile error message saying Loop without Do. If you look closely throughout the VBA code, you probably observe that Do is missing before While. For your convenience, to run VBA code you need to launch VBA Macro editor.
Fixed VBA Code:
Sub PythagoreanNumber()
Dim a As Integer
Dim b As Integer
Dim count As Integer
Dim result As Integer
count = 0
a = 1
Do While a <= 100
For b = 1 To 100
Csqr = a ^ 2 + b ^ 2
If IsNumeric(Sqr(Csqr)) And Sqr(Csqr) = Int(Sqr(Csqr)) Then
count = count + 1
result = Sqr(Csqr)
MsgBox "Pythagorean Number " & count & " is " & result & " (" & a & "^2 + " & b & "^2)"
If count = 10 Then Exit Sub
End If
b = b + 1
Next b
a = a + 1
Loop
End Sub
Inserting Do and fixing the VBA code, we are now able to execute the VBA code. Thus we obtain to get 1st Pythagorean number 5 in the pop-up message box. thus, we get 10 Pythagorean numbers sequentially.
Reason 2. For Without “Next” Statement Inside Do Loop in VBA
As you can see in the above picture, For loop is a nested loop of the Do While loop here. The thing is we must write next to execute the For loop repeatedly. Although, you have probably noticed that Next b is missing in the code that leads to Compile Error: Loop without Do.
Fixed VBA Code:
Sub PythagoreanNumber()
Dim a As Integer
Dim b As Integer
Dim count As Integer
Dim result As Integer
count = 0
a = 1
Do While a <= 100
For b = 1 To 100
Csqr = a ^ 2 + b ^ 2
If IsNumeric(Sqr(Csqr)) And Sqr(Csqr) = Int(Sqr(Csqr)) Then
count = count + 1
result = Sqr(Csqr)
MsgBox "Pythagorean Number " & count & " is " & result & " (" & a & "^2 + " & b & "^2)"
If count = 10 Then Exit Sub
End If
b = b + 1
Next b
a = a + 1
Loop
End Sub
Inserting Next b we were finally able to fix the VBA code and achieve the output of 1st Pythagorean Number 5 with a pop-up message box. Consequently, we get the rest of the 9 Pythagorean numbers too.
Note: You would expect the error For without Next, but VBA shows Loop without Do error because of the nested loop of Do While loop.
Reason 3. IF Without End IF Inside Do Loop in VBA
Here, we wrote a VBA code to add all the odd numbers that are inserted through an input box and separated by a comma. Here, we use an IF condition under a Do While loop. Hence, forgot to add an End If statement that delineates Compile error: Loop Without Do.
Fixed VBA Code:
Sub Add_all_odd_numbers()
Dim p As String
Dim num() As String
Dim i As Integer
Dim sum As Integer
p = InputBox("Input your numbers: ")
num = Split(p, ",")
i = 0
Do While i <= UBound(num)
If num(i) Mod 2 = 1 Then
sum = sum + num(i)
End If
Loop
If sum > 0 Then
MsgBox "Total: " & sum
Else
MsgBox "No odd number found"
End If
End Sub
By writing End If statement to close the condition, we are likely to fix the code and get the Inputbox as an outcome. After that, by Inserting 2,3,4,5,6 in the input box field and hitting the OK button, we obtain 8, the sum of two odd numbers 3 and 5.
Note: You would expect the error IF without End If, but VBA shows Loop without Do error because of the use of the Do While loop.
How to Fix Do Without Loop Error in Excel VBA
If you look closely throughout the VBA code, you probably observe that Loop is missing as we use Do While loop. Absence of Loop leads to Compile error: Do without Loop.
Fixed VBA Code:
Sub PythagoreanNumber()
Dim a As Integer
Dim b As Integer
Dim count As Integer
Dim result As Integer
count = 0
a = 1
Do While a <= 100
For b = 1 To 100
Csqr = a ^ 2 + b ^ 2
If IsNumeric(Sqr(Csqr)) And Sqr(Csqr) = Int(Sqr(Csqr)) Then
count = count + 1
result = Sqr(Csqr)
MsgBox "Pythagorean Number " & count & " is " & result & " (" & a & "^2 + " & b & "^2)"
If count = 10 Then Exit Sub
End If
b = b + 1
Next b
a = a + 1
Loop
End Sub
Inputting Loop before End Sub statement and executing the VBA code, we get 1st Pythagorean Number 5. Further, we obtain another 9 Pythagorean numbers accordingly.
Frequently Asked Questions(FAQ)
1. What is a do loop in VBA?
Do loop allows the loop within a macro that repeats tasks until a specific value or criteria is achieved. The programmers can set the loop to repeat a specific number of times until a variable exceeds a specific value.
2. Why do loop without do error occur?
3 common reasons that lead to Compile error: Loop without Do.
- If you forget to insert Do before While during the use of loops.
- Miss of Next while using the For loop in a nested loop containing the Do loop.
- Absence of End IF statement under a Do loop.
3. How do you exit a do while loop in VBA?
You can close any Do Until or Do While loop by applying the Exit Do statement.
Takeaways from This Article
📌 Absence of Do before While or Until heading towards Compile error.
📌 Miss of Next during the use of For Loop in a nested loop containing Do loop leads to loop without do error.
📌 Absence of an End If statement within a Do loop accelerates to an error.
📌 Similarly, miss of Loop leads to a Do without loop error.
Conclusion
We concisely construe the reasons and solutions for VBA Loop without Do error. I hope you enjoyed your learning and will be able to eliminate these errors by fixing your VBA code. Any suggestions including queries are appreciated. Don’t hesitate to leave your thoughts in the comment section. For more learning, don’t forget to explore our site.