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

vba
Sub 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 Sub

Press 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:

vba
Sub 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 Sub

Set 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

  1. Debug a loop program with breakpoints
  2. Check variable values in the Immediate Window
  3. Track array changes with the Watch Window

Exercise Solutions

  1. Press F9 inside the loop body, execute line by line with F8, and watch the variables change.
  2. Type ?variableName in the Immediate Window, or add Debug.Print variableName.
  3. Add watch expressions in the Watch Window to follow every array element.

Common Errors and Troubleshooting

  1. F8 does nothing — focus is not in the code window, or a macro is already running; click into the code window first.
  2. 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).
  3. ?variableName in 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.
  4. Stepping suddenly raises Run-time error '9': Subscript out of range — you just stepped onto arr(i) with i out of bounds (the array is 1 To 5 but the code touches arr(0)); a perfect moment to fix it.
  5. 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

  1. Step all the way through DebugExample_Array with F8 and add watches on arr(1) through arr(5) to watch each assignment.
  2. Run ?Worksheets.Count and ?ThisWorkbook.Name in the Immediate Window — practice "asking" the program without writing any code.
  3. Add Debug.Print i, sum to the loop body of DebugExample_Loop, then review the full running-total trace in the Immediate Window afterwards.

由在线工具箱(www.vba.net)整理制作