Today's Goals
- Master breakpoint debugging
- Learn to use the Watch Window
- Learn to use the Immediate Window
Key Concepts
1. Debugging Keys
- F9: set/clear a breakpoint
- F8: Step Into (execute one statement)
- Shift+F8: Step Over
- Ctrl+F8: Run to Cursor
The standard VBA breakpoint workflow: press F9 on the line you suspect (a red dot appears in the margin), run the macro so it stops there and enters break mode, then press F8 to walk through line by line, hovering over any variable to see its current value. F8 (Step Into) also steps inside a called procedure; if you want to treat the call as a single step, use Shift+F8 (Step Over); to jump straight to a specific line and inspect the state there, use Ctrl+F8.
2. Windows
- Watch Window: keep an eye on variable values
- Immediate Window: run code, print values
The Watch Window (View → Watch Window) tracks an expression through "Add Watch", so every change of value is visible at a glance. In the Immediate Window (Ctrl+G) you type ?variableName in break mode and press Enter to print its value; expressions like ?Range("A1").Value work too, and you can even execute a statement directly. In code, Debug.Print x sends values to the Immediate Window without interrupting execution — ideal for tracing how a variable evolves inside a loop.
Today's Code
vbaSub DebugExample_Loop()
Dim i As Integer
Dim sum As Integer
sum = 0
For i = 1 To 10
sum = sum + i
Next i
MsgBox "Sum of 1 to 10: " & sum
End SubPress F9 on the sum = sum + i line, run, then step with F8 and type ?sum in the Immediate Window after each pass to watch the running total — the most direct way to hunt down loop arithmetic bugs. The DebugExample_Conditional and DebugExample_Array procedures in the .bas file can be practiced the same way, for conditional branching and array assignments.
Real-World Scenario
A summing routine is always off by 1 — use a breakpoint to find out why:
vbaSub SumDebug()
Dim i As Integer
Dim sum As Integer
sum = 1 ' Bug: the initial value should be 0
For i = 1 To 10
sum = sum + i
Next i
MsgBox "Sum of 1 to 10: " & sum ' Prints 56, should be 55
End SubSet a breakpoint on sum = sum + i, step two passes with F8, and notice that sum is 2 after the first pass instead of 1 — walking back to the initialization reveals the bug. The debugging mindset: breakpoints narrow the range → hover or the Immediate Window shows the state → compare against expectations to spot the difference.
Exercises
- Debug a loop program with breakpoints
- Check variable values in the Immediate Window
- Track array changes with the Watch Window
Exercise Solutions
- Press F9 inside the loop body, execute line by line with F8, and watch the variables change.
- Type
?variableNamein the Immediate Window, or addDebug.Print variableName. - Add watch expressions in the Watch Window to follow every array element.
Common Errors and Troubleshooting
- F8 does nothing — focus is not in the code window, or a macro is already running; click into the code window first.
- A breakpoint never fires — it sits on a comment or blank line, or that line is never executed (the other side of a conditional branch).
?variableNamein the Immediate Window reports "variable not defined" — the variable does not exist in the procedure currently in break mode, or it is out of scope.- Stepping suddenly raises
Run-time error '9': Subscript out of range— you just stepped ontoarr(i)with i out of bounds (the array is1 To 5but the code touchesarr(0)); a perfect moment to fix it. - Editing code in break mode warns that the action will reset your project — the edit ends break mode and breakpoints must be set again; that is normal.
Extension Exercises
- Step all the way through
DebugExample_Arraywith F8 and add watches onarr(1)througharr(5)to watch each assignment. - Run
?Worksheets.Countand?ThisWorkbook.Namein the Immediate Window — practice "asking" the program without writing any code. - Add
Debug.Print i, sumto the loop body ofDebugExample_Loop, then review the full running-total trace in the Immediate Window afterwards.
由在线工具箱(www.vba.net)整理制作