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?

2. Your macro runs without an error but gives the wrong answer. What is the best first step?

3. You want to debug a loop that processes 30 records. Where should you set your breakpoint?

4. What does the Watch Window show you?

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?

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.

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

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]"