Setting the next statement in Excel VBA allows you to control which line of code executes next during debugging, enabling you to skip sections, re-run lines, or jump to specific points without restarting the macro. This is useful for testing logic, bypassing errors, or inspecting variable states at precise moments. You need the VBA Editor open with a running macro in break mode.
What You'll Need
- Microsoft Excel with a VBA macro project
- The VBA Editor (accessed via Alt + F11)
- A macro currently running or paused in break mode (e.g., after setting a breakpoint or using Stop)
Step-by-Step
- Open your Excel workbook and press Alt + F11 to launch the VBA Editor.
- In the Project Explorer window, locate and double-click the module containing the macro you want to debug.
- Set a breakpoint by clicking in the gray margin to the left of the line of code where you want execution to pause, or press F9 with the cursor on that line.
- Return to Excel and run the macro (e.g., via Developer > Macros, or assign it to a button). Execution will pause at the breakpoint.
- In the VBA Editor, ensure the code window is active and the yellow execution pointer is visible on the paused line.
- Use your mouse to click and drag the yellow execution pointer (the arrow in the gray margin) to any other line of code in the same procedure.
- Alternatively, right-click on the target line of code and select "Set Next Statement" from the context menu.
- Press F8 to step through the next line, or F5 to continue execution from the new position.
- To resume normal execution flow, continue debugging or stop and restart the macro as needed.
Common Mistakes
- Trying to set the next statement to a line in a different procedure — this is not allowed and will result in an error.
- Attempting to set the next statement while code is not in break mode (i.e., running normally or ended).
- Dragging the execution pointer into a line that contains only comments or whitespace — VBA will not allow this and may snap back.
- Assuming that setting the next statement resets variables — it does not; variable values remain unchanged from their current state.
- Forgetting that you cannot set the next statement to a line inside a With block from outside it, or vice versa, due to scope restrictions.
Tips
- Use Set Next Statement to skip over problematic code sections (e.g., a problematic loop or external call) during testing.
- Combine this technique with the Immediate Window (? variableName) to inspect or modify variable values before continuing.
- If you need to re-execute a line, set the next statement to an earlier line and step through with F8.
- You can set the next statement to a line ahead of the current position to skip ahead, useful for bypassing initialization you’ve already tested.
- Always verify the syntax and logic of the target line before jumping to it — jumping into the middle of a loop or error handler can cause unexpected behavior.
Troubleshooting
If you cannot set the next statement:
- Confirm the macro is paused in break mode — look for the yellow execution pointer. If not paused, set a breakpoint or use Stop.
- Ensure you are trying to set the next statement within the same procedure — cross-procedure jumps are not permitted.
- Check that the target line is executable code (not a comment, blank line, or declaration only).
- If the option is grayed out, the debugger may be in a restricted state — try stopping the macro (Shift + F5) and restarting.
- If VBA crashes or behaves oddly after jumping, reset the session: stop debugging, close and reopen the VBA Editor.
Frequently Asked Questions
Can I set the next statement to a line in another module?
No, you can only set the next statement to a line within the same procedure. Jumping between procedures or modules is not supported by the VBA debugger.
What happens to variables when I set the next statement?
Variable values remain exactly as they were before the jump. No values are reset or re-initialized unless the line you jump to explicitly reassigns them.
Can I use Set Next Statement to avoid an error?
Yes, you can skip the line that causes an error by setting the next statement to a later line, but the underlying issue remains — use this for testing, not as a fix.
Is there a keyboard shortcut to Set Next Statement?
There is no direct shortcut, but you can right-click and choose the command, or drag the yellow execution pointer with the mouse.
Why does the yellow arrow snap back when I try to move it?
This happens if you try to move the execution pointer to an invalid line (e.g., in another procedure, a comment, or a non-executable line). Only executable statements in the current procedure are valid targets.
Can I set the next statement during design time (when not debugging)?
No, the Set Next Statement feature only works when code is paused in break mode during debugging.