The "Loop without Do" error in VBA (run-time error 91 or a compile error) means your code has a Loop statement that isn't paired with a matching Do statement earlier in the same procedure. VBA requires every Loop to close a Do...Loop block. To fix it, find the orphaned Loop and either add the missing Do line above it or remove the Loop if it was left over from deleted code. The quickest check is to press Ctrl+F in the VBA editor, search for "Loop", and verify each one has a corresponding "Do" above it at the same nesting level.
What You'll Need
- Access to the VBA editor (Alt+F11 in Excel, Word, Access, or any Office app).
- The macro or module that triggers the error, open in the editor.
- Basic familiarity with
Do...LoopandFor...Nextsyntax.
Step-by-Step
- Reproduce the error and note the line. Run the macro (F5) or step through it (F8). When the error dialog appears, click Debug. The editor highlights the offending line, usually a
Loopstatement. Write down the line number or the procedure name so you can return to it. - Search for every Loop in the procedure. Press Ctrl+F, type
Loop, and click Find Next repeatedly. For each hit, look upward in the same procedure for aDostatement at the same indentation level. ADocan appear asDo,Do While ...,Do Until ..., orDo ... Loop While .... If you find aLoopwith no matchingDoabove it, that is your culprit. - Decide whether the Do is missing or the Loop is extra. Two common cases: (a) you deleted the
Doline while editing but left theLoop; (b) you pasted a block of code and theDostayed behind in the old location. If the loop body still makes sense, add the correctDoline above it. If the loop is no longer needed, delete the orphanedLoopline entirely. - Add the missing Do correctly. Place the
Doimmediately above the first line of the loop body, at the same indentation as theLoop. For a condition-checked-at-top loop, writeDo While conditionorDo Until condition. For a condition-checked-at-bottom loop, write plainDoand put the condition on theLoopline:Loop While condition. Do not mix both forms in the same block. - Check nesting with If...End If and For...Next. A
Loopinside anIfblock still needs its ownDoinside that same block. If you close anIfbefore theLoop, VBA sees theLoopas orphaned. Move theDoandLoopso they sit inside the sameIf...End Ifpair, or move theLoopoutside theIfif that matches your logic. - Compile before running. In the VBA editor, choose Debug > Compile VBAProject. A compile error points to the exact line and catches mismatched
Do/Looppairs before the macro runs. Fix any highlighted line and compile again until no errors appear. - Step through the fixed loop. Press F8 to execute line by line. Watch the loop execute at least once and confirm it exits. If it runs forever, add an exit condition or a counter that stops after a set number of iterations.
Common Mistakes
- Using Loop with For...Next. A
For...Nextloop closes withNext, notLoop. If you converted aForloop to aDoloop, change the closing line too. - Mixing Do While and Loop Until. VBA allows
Do While ... LoopandDo ... Loop While, but notDo While ... Loop Until. The condition style must match at both ends. - Deleting the Do line accidentally. When you delete a block of code, the
Docan be removed while theLoopremains. Always delete loop pairs together. - Indentation hiding the problem. VBA ignores indentation, so a
Loopthat looks aligned may belong to a different block. Use the editor's indent guides or temporarily indent every line manually to see the structure. - Pasting code with mismatched line endings. Copying from a web page can drop the
Doline if it was on a wrapped line. Re-type theDoline rather than pasting it.
Tips
- Use
Do Whilewhen the condition should be checked before the first iteration; useDo ... Loop Whilewhen the body must run at least once. - Add a comment above each
Dodescribing the exit condition. It makes orphanedLooplines obvious later. - Keep loop bodies short. Long bodies make it harder to see which
Loopbelongs to whichDo. - Use
Exit Doinside the loop for early exit instead of adding complex conditions to theLoopline. - Turn on Require Variable Declaration (Tools > Options > Editor) to catch other structural errors early.
Troubleshooting
- Error persists after adding Do. Check for a second orphaned
Loopelsewhere in the procedure. Search again from the top. - Compile error points to a different line. VBA sometimes reports the error at the next statement after the real problem. Look one or two lines above the highlighted line for a missing
Do. - Loop runs forever. The condition never becomes false. Add a counter variable and an
If counter > 1000 Then Exit Doguard, or verify the variable in the condition is actually updated inside the loop. - Error only in one Office app. The VBA editor is shared, but references can differ. Check Tools > References for missing libraries that might cause odd compile behavior.
- Code works in one module but not another. Copy the entire procedure into a fresh module and compile. Hidden characters from pasting can cause structural errors.
Frequently Asked Questions
What causes "Loop without Do" in VBA?
A Loop statement exists without a matching Do statement earlier in the same procedure. This happens after deleting code, pasting incomplete blocks, or confusing For...Next with Do...Loop.
Can I use Loop with For...Next?
No. For...Next closes with Next. Using Loop to close a For loop triggers this error. Change the closing line to Next or convert the loop to Do...Loop.
How do I find the orphaned Loop quickly?
Press Ctrl+F, search for "Loop", and check each hit for a Do above it at the same nesting level. Compiling the project (Debug > Compile VBAProject) also highlights the line.
Does indentation affect VBA loops?
No. VBA ignores indentation, so a Loop that looks aligned may still be orphaned. Use comments and short loop bodies to keep structure clear.
What is the difference between Do While and Do Until?
Do While runs while the condition is true; Do Until runs until the condition becomes true. Both must be paired with a plain Loop or a matching Loop While/Loop Until at the other end.
Can I have nested Do...Loop blocks?
Yes, but each Loop must match its own Do. Indent nested blocks and compile after each change to catch mismatches early.