Fix Word VBA Debugger Step-Into Skipping Lines on Long Procedures
🔍 WiseChecker

Fix Word VBA Debugger Step-Into Skipping Lines on Long Procedures

When you press F8 to step through a long VBA macro in Word, the debugger sometimes jumps over several lines of code without executing them. This makes it impossible to trace variable changes or confirm logic flow. The root cause is a known performance optimization in the VBA engine that skips lines it considers “dead code” or unreachable during rapid stepping. This article explains why the debugger skips lines, provides a reliable fix to force single-line stepping, and covers related issues such as breakpoints not triggering or procedures that appear to hang.

Key Takeaways: Forcing the VBA Debugger to Step Through Every Line

  • VBA Editor > Tools > Options > General > Break on All Errors: Prevents the debugger from skipping lines after a silent error is caught and handled.
  • Add DoEvents after each logical block: Forces the debugger to pause and process user interface events, which prevents line skipping in long loops.
  • Compile the project before debugging: Ensures all variables are declared and syntax errors are caught, eliminating one cause of skipped lines.

ADVERTISEMENT

Why the VBA Debugger Skips Lines in Long Procedures

The VBA debugger in Word uses a just-in-time compilation strategy for performance. When you step through a long procedure, the debugger compiles only the current block of code. If it detects that a line has no side effects—for example, a comment, a redundant assignment, or a variable that is never used later—it may skip that line to speed up execution. This is not a bug; it is an intentional optimization that becomes problematic when you need to verify every single step.

A second cause is error handling. If your procedure uses On Error Resume Next, the debugger treats errors as non-breaking events. After an error occurs, the debugger may jump multiple lines to continue execution, skipping the lines that would have run if the error had not occurred. Similarly, if you have a GoTo statement or an Exit Function inside a loop, the debugger can skip all remaining lines in the current block.

A third cause is the use of line labels or continuation characters. The debugger may evaluate a line label as a no-op and skip directly to the next executable statement. This is especially common in long procedures that contain multiple Select Case or If...ElseIf...End If blocks.

Steps to Stop the Debugger From Skipping Lines

The following steps force the VBA editor to step through every line, regardless of optimization or error-handling logic. Perform these steps in the order shown.

  1. Open the VBA editor and set Break on All Errors
    In the VBA editor, click Tools > Options. On the General tab, under Error Trapping, select Break on All Errors. Click OK. This setting overrides any On Error Resume Next statements and stops execution at the exact line where an error occurs, preventing the debugger from skipping lines after a silent error.
  2. Compile the project before starting
    In the VBA editor, click Debug > Compile VBAProject. Fix any syntax errors or undeclared variables that appear. Compilation removes dead code paths that the debugger might skip. After compilation, press F8 to begin stepping through the procedure.
  3. Insert DoEvents after each major block
    In long procedures, add DoEvents after each For loop, While loop, or If block. For example, after End If or Next i, add a new line with DoEvents. This forces the debugger to yield to the operating system and process any pending events, which breaks the optimization that skips lines. The debugger will now stop on the DoEvents line itself and then on the next executable line.
  4. Remove all On Error Resume Next statements temporarily
    If your procedure uses On Error Resume Next, comment it out by adding an apostrophe at the start of the line. Replace it with On Error GoTo 0 to restore default error handling. This prevents the debugger from skipping lines after an error. After debugging, uncomment the original error handler.
  5. Set a breakpoint at the first line and step line by line
    Click in the left margin of the first executable line of the procedure to add a red breakpoint dot. Press F5 to run the macro until the breakpoint. Then press F8 repeatedly. The debugger should now stop at every line, including comments if they are the only content on a line. If a line is still skipped, check whether it contains a line label or a continuation character (underscore).

ADVERTISEMENT

If the Debugger Still Skips Lines After the Main Fix

Breakpoints do not trigger on certain lines

If a breakpoint turns gray or does not stop execution, the line is likely inside an inactive code path. For example, a breakpoint inside an ElseIf block that never evaluates to true will not trigger. Move the breakpoint to a line that always executes, such as the Sub or Function declaration line. Alternatively, use Debug.Print statements to output variable values to the Immediate window instead of relying on breakpoints.

Word freezes or becomes unresponsive while stepping

Long procedures that manipulate Word objects—such as Selection.Find or ActiveDocument.Tables—can cause the debugger to hang. Add a DoEvents call inside the loop and reduce the screen updating overhead by setting Application.ScreenUpdating = False at the start of the macro. Set it back to True after the loop. This does not fix skipping lines, but it prevents the debugger from becoming unresponsive.

The debugger jumps from a loop to the end of the procedure

This happens when the loop condition becomes false unexpectedly. Check the loop counter variable in the Immediate window by typing ?counterVariable and pressing Enter. If the variable is out of range, the loop exits early and the debugger skips the remaining iterations. To avoid this, add a watch on the loop variable: right-click the variable name in the code window and select Add Watch. Set the watch type to Break When Value Changes.

Debugger Behavior Before Fix (Line Skipping) After Fix (Forced Single-Step)
Error handling Skips lines after On Error Resume Next Breaks on every error line
Loop optimization Skips lines in long For or While loops Stops on every line including DoEvents
Compilation Skips lines with undeclared variables Compiles all code, no dead paths
Line labels Skips directly to next executable line Stops on label line if it is the only content

By enabling Break on All Errors, compiling the project, and adding DoEvents calls, you can force the Word VBA debugger to step through every line of a long procedure. Use the Immediate window to inspect variables at each stop. For procedures that still skip lines, check for inactive code paths and remove temporary error handlers. These steps give you full control over the debugging process and help you identify logic errors that would otherwise go unnoticed.

ADVERTISEMENT