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...Loop and For...Next syntax.

Step-by-Step

  1. 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 Loop statement. Write down the line number or the procedure name so you can return to it.
  2. 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 a Do statement at the same indentation level. A Do can appear as Do, Do While ..., Do Until ..., or Do ... Loop While .... If you find a Loop with no matching Do above it, that is your culprit.
  3. Decide whether the Do is missing or the Loop is extra. Two common cases: (a) you deleted the Do line while editing but left the Loop; (b) you pasted a block of code and the Do stayed behind in the old location. If the loop body still makes sense, add the correct Do line above it. If the loop is no longer needed, delete the orphaned Loop line entirely.
  4. Add the missing Do correctly. Place the Do immediately above the first line of the loop body, at the same indentation as the Loop. For a condition-checked-at-top loop, write Do While condition or Do Until condition. For a condition-checked-at-bottom loop, write plain Do and put the condition on the Loop line: Loop While condition. Do not mix both forms in the same block.
  5. Check nesting with If...End If and For...Next. A Loop inside an If block still needs its own Do inside that same block. If you close an If before the Loop, VBA sees the Loop as orphaned. Move the Do and Loop so they sit inside the same If...End If pair, or move the Loop outside the If if that matches your logic.
  6. Compile before running. In the VBA editor, choose Debug > Compile VBAProject. A compile error points to the exact line and catches mismatched Do/Loop pairs before the macro runs. Fix any highlighted line and compile again until no errors appear.
  7. 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...Next loop closes with Next, not Loop. If you converted a For loop to a Do loop, change the closing line too.
  • Mixing Do While and Loop Until. VBA allows Do While ... Loop and Do ... Loop While, but not Do While ... Loop Until. The condition style must match at both ends.
  • Deleting the Do line accidentally. When you delete a block of code, the Do can be removed while the Loop remains. Always delete loop pairs together.
  • Indentation hiding the problem. VBA ignores indentation, so a Loop that 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 Do line if it was on a wrapped line. Re-type the Do line rather than pasting it.

Tips

  • Use Do While when the condition should be checked before the first iteration; use Do ... Loop While when the body must run at least once.
  • Add a comment above each Do describing the exit condition. It makes orphaned Loop lines obvious later.
  • Keep loop bodies short. Long bodies make it harder to see which Loop belongs to which Do.
  • Use Exit Do inside the loop for early exit instead of adding complex conditions to the Loop line.
  • 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 Loop elsewhere 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 Do guard, 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.