F8 Debugging Practice
Module 8 of 9
REMINDER: To get the most out of this module, you should have already watched the Macro Demo videos in Canvas and followed along with the Macro Demo file. This practice will build upon that foundation.
What F8 Actually Does
When a macro doesn't work, most students read the code looking for the problem. That rarely works. F8 does something completely different — it runs your macro one line at a time and shows you what Excel actually does at each step. You watch the cursor move, watch variables change value, and watch conditions evaluate in real time. The bug reveals itself because you can see exactly where the code stops doing what you expected.
Professor Sanders said it best: lightbulbs go off when students start doing this. It changes how you think about VBA. A macro isn't a wall of text — it's a sequence of actions you can step through and observe. Once you've used F8 to find a bug, reading code passively feels like trying to find a spelling mistake by staring at a paragraph instead of reading it word by word.
If you only learn one thing from this site, make it F8. Every exam question that asks "what does this macro do" becomes straightforward once you've practiced stepping through code and watching what actually happens.
Setting Up Your Screen
The key to F8 debugging is having both windows visible at the same time — the VBA Editor on one side, Excel on the other. When you step through code, you need to see where the cursor moves in Excel and watch variable values change in the Watch Window simultaneously. If you can only see one window at a time, you're missing half the information.
Setup
1. Open Excel → press Alt+F11 to open the VBA Editor 2. VBA Editor: Window → Tile Vertically (or drag windows side by side) 3. VBA Editor: View → Watch Window 4. Result: Excel on one side, VBA Editor + Watch Window on the other
How to Add a Watch
Highlight any variable name in your code and drag it down to the Watch Window — or right-click and choose Add Watch. The Watch Window shows the variable's current value and data type, updating live as you step through with F8. When debugging an IF condition, add both sides of the comparison so you can see exactly what each side contains when it evaluates.
How to Set a Breakpoint
Click the grey margin bar to the left of any line to set a breakpoint — a red dot appears. Press F5 to run the macro at full speed; it pauses at the breakpoint. Then use F8 to step one line at a time from that point. Press F9 to toggle a breakpoint on/off, or click the red dot to remove it.
Set your breakpoint at the start of the loop, not the start of the macro. Running full-speed to the loop and then stepping through is much faster than F8-ing through every line of setup code to get there.
Quick Check
1. You press F8 once inside the VBA Editor. What happens?
Each F8 keypress executes exactly one line of code. This lets you watch what happens in Excel — cursor movement, cell values, variable changes — after each individual instruction.
2. Your macro runs without an error but gives the wrong answer. What is the best first step?
A macro that runs without error but produces wrong results has a logic error — the code is valid VBA but the logic is wrong. F8 with the Watch Window is the right tool: you watch variables and see exactly where the behavior diverges from what you expected.
3. You want to debug a loop that processes 30 records. Where should you set your breakpoint?
Setting the breakpoint at the loop lets the setup code run at full speed (F5), then you step through the loop itself with F8. Stepping from line 1 means pressing F8 through every navigation and variable declaration before you even reach the interesting part.
4. What does the Watch Window show you?
The Watch Window monitors any variables you add to it and shows their value and data type, updating after each F8 step. This is how you catch a variable that isn't changing the way you expect — or is holding the wrong value when an IF condition evaluates.
5. A macro runs and produces the wrong number. You step through with F8 and watch a counter variable in the Watch Window. After the first loop iteration it shows 100, after the second it shows 200, after the third it shows 300. The correct value after three iterations should be 600. What type of bug is this?
The counter is showing the current row's value each time instead of adding to
a running total. The line is probably Total = ActiveCell when it
should be Total = Total + ActiveCell. This is the most common
accumulation bug and exactly the type F8 catches immediately.
Question 2 is the mindset shift. When there's no error message, students often assume the code is fine and look for a data problem. A logic bug means the code runs perfectly — it just does the wrong thing. F8 is the only reliable way to find it.
Easy Wins
Guided Walkthrough — Find the Bug
The macro below runs without an error message but gives the wrong answer. Your job is to find the bug using F8 and the Watch Window. Follow each step exactly — this is the technique you'll use on the exam.
Copy this macro into a new module in Excel. It is a modified version of the CumulativeTotal macro from the Macro Handout. It should add up all the values in column A and display the total — but it gives the wrong answer. Don't try to spot the bug by reading. Follow the steps.
Create a worksheet named "Totals" and put the values 100, 200, 300, 400, 500 in cells A2 through A6. Column A must have the data — this is where the macro starts and loops.
Option Explicit
Sub CumulativeTotal_Broken()
Dim CumulativeTotal As Double
Sheets("Totals").Select
Range("A2").Select
Do Until ActiveCell = ""
CumulativeTotal = ActiveCell
ActiveCell.Offset(1, 0).Select
Loop
MsgBox "The cumulative total is " & CumulativeTotal
End Sub
Press F5 to run the macro normally. Write down the number in the MsgBox. With the values 100, 200, 300, 400, 500 — the correct total is 1500. The macro will show 500 instead (only the last value, not the accumulated sum). Now you know what you're looking for.
In the VBA Editor, highlight the word CumulativeTotal anywhere in the code.
Drag it to the Watch Window. You should see it appear with Value = 0 and
Type = Double. Now you can watch it change as the macro runs.
Click the grey margin bar to the left of the Do Until ActiveCell = "" line.
A red dot appears. This is where the macro will pause when you press Play.
Press F5. The macro pauses at your breakpoint. Now press F8 three times slowly:
- First F8: the Do Until condition evaluates — watch the cursor jump to Excel
- Second F8: the
CumulativeTotal = ActiveCellline executes — look at the Watch Window. What is CumulativeTotal now? - Third F8:
ActiveCell.Offset(1, 0).Selectexecutes — the cursor moves down
Press F8 two more times to complete the second loop iteration. What is CumulativeTotal after the second iteration? It should be 300 if accumulating correctly (100 + 200). Is it?
You should have noticed that CumulativeTotal after iteration 1 = 100, and after iteration 2 = 200 — not 300. It's replacing the total each time instead of adding to it.
The bug is on this line:
CumulativeTotal = ActiveCell
It should be:
CumulativeTotal = CumulativeTotal + ActiveCell
Make that one change. Clear the breakpoint (click the red dot). Press F5. The MsgBox should now show 1500.
Complete Fixed Code:
Option Explicit
Sub CumulativeTotal_Fixed()
Dim CumulativeTotal As Double
Sheets("Totals").Select
Range("A2").Select
CumulativeTotal = 0
Do Until ActiveCell = ""
CumulativeTotal = CumulativeTotal + ActiveCell
ActiveCell.Offset(1, 0).Select
Loop
MsgBox "The cumulative total is " & CumulativeTotal
End Sub
What you just practiced:
- Setting a breakpoint to pause at a specific line
- Using the Watch Window to monitor a variable's value
- Stepping with F8 to see what each line actually does
- Identifying a logic error by watching the variable NOT change the way you expected
Practice Problem
Same technique, no step-by-step this time. This macro should find all students with a GPR above 3.5 and count them — but it runs and always shows 0. Use F8 and the Watch Window to find the bug. Fix it with one line change.
Use the Aggie Advisors data you've already pasted into Excel, or the data from the Variables module. Your data should have StudentID in column A and TAMU_GPR in column D.
Option Explicit
Sub CountHighGPR_Broken()
Dim HighCount As Integer
Sheets("Sheet1").Select
Range("A2").Select
Do Until ActiveCell = ""
If ActiveCell > 3.5 Then
HighCount = HighCount + 1
End If
ActiveCell.Offset(1, 0).Select
Loop
MsgBox "Students with GPR above 3.5: " & HighCount
End Sub
Add a Watch for both ActiveCell and ActiveCell.Offset(0, 3) before
stepping through. Look at what each one actually contains when the IF
evaluates. One of them is the GPR — which one?
The bug is If ActiveCell > 3.5 — this checks column A (StudentID),
not column D (GPR). A 9-digit student ID will never be greater than 3.5.
Fix: If ActiveCell.Offset(0, 3) > 3.5 Then
Expected result with Aggie Advisors data: Students with GPR above 3.5: 16
Challenge
This is exam level. The macro below is a broken version of the AddNewStudents macro from the Aggie Advisors project. It runs without an error but produces wrong results — it adds the wrong number of students and the count in the final MsgBox is incorrect.
There are two bugs. Find both using F8 and the Watch Window. Fix each with the minimum change possible — one line per bug. No hints. No steps. This is exam level.
Option Explicit
Sub AddNewStudents_Broken()
Dim NewGroup As Integer
Dim NumberAccepted As Integer
Dim StudentID As Long
Dim GPR As Double
Dim LastName As String
Dim FirstName As String
Sheets("Applicant Information").Select
Range("A2").Select
If ActiveCell = "" Then
MsgBox "No applicants found."
Exit Sub
End If
NewGroup = InputBox("Enter the new group number:")
Do Until ActiveCell = ""
If ActiveCell.Offset(0, 6) = "Accept" Then
StudentID = ActiveCell
LastName = ActiveCell.Offset(0, 1)
FirstName = ActiveCell.Offset(0, 2)
GPR = ActiveCell.Offset(0, 3)
Sheets("Student Information").Select
ActiveCell = StudentID
ActiveCell.Offset(0, 1) = NewGroup
ActiveCell.Offset(0, 2) = "U"
ActiveCell.Offset(0, 3) = LastName
ActiveCell.Offset(0, 4) = FirstName
ActiveCell.Offset(0, 5) = GPR
NumberAccepted = NumberAccepted + 1
ActiveCell.Offset(1, 0).Select
Sheets("Applicant Information").Select
End If
ActiveCell.Offset(1, 0).Select
Loop
MsgBox NumberAccepted & " students added for Group " & NewGroup
End Sub
What the correct output should be:
- 20 students added (all Accept decisions from the 30-record dataset)
- MsgBox: "20 students added for Group [X]"