Stepping through Code

In most cases, you design code to run with little or no user intervention. However, when you're testing code, sometimes you want to do more than insert a breakpoint or two or include a couple of Debug .Print statements. If you're running code with many variable changes or some intricate looping, it can sometimes be helpful to step through the code line by line. Doing this allows you to watch the value of variables after each line of code is executed. This can help you pinpoint any errors or logical mistakes in the code.

To step through your code, press F8 to begin the procedure (you can also press F8 after the code has entered break mode to step through the remaining code). When you first press F8 to begin code execution, the name of the sub or function is highlighted in yellow. Subsequent clicks of the F8 key move execution from line to line, highlighting the next executable line in yellow. Comment lines and Dim statements are skipped when stepping through code. As you press F8, the highlighted line is executed.

If the current procedure calls another sub or function, F8 will also execute the called procedure line by line. If you're confident that the called procedure doesn't contain any errors and you want code execution to process the entire called procedure and then return to line by line execution of the calling procedure, click Shift+F8 to step over the procedure. Stepping over the called procedure executes the entire procedure and then returns to the calling procedure where code execution continues one step at a time. If you're within a called procedure, you can also click Ctrl+Shift+F8 to step out of the current procedure. So what's the difference between stepping over and stepping out of the procedure? Well, if you're already in the called procedure, the two are exactly the same. However, let's assume you're stepping through the following code:

Option Compare Database

Private blnUnderBudget As Boolean

Const curBudget = 10 0 0

Private Sub GoShopping()

Dim intSuits As Integer

Dim curSuitPrice As Currency

Dim curTotalPrice As Currency

curSuitPrice = 100

intSuits = InputBox("Enter the desired number of suits",


For i = 1 To intSuits

curTotalPrice = curTotalPrice + curSuitPrice

If curTotalPrice > curBudget Then

blnUnderBudget = False


blnUnderBudget = True

End If


If blnUnderBudget = False Then


End If

End Sub

Private Sub OverBudget()

Debug.Print "You've gone over budget."

Debug.Print "You need to work some overtime."

Debug.Print "Remember to pay your taxes."

End Sub

Use the F8 key to step through the code until you reach the last If...Then loop (If blnUnderBudget = False Then). When the OverBudget line is highlighted in yellow (meaning it hasn't yet been executed), stepping over the OverBudget procedure returns execution to the line after the OverBudget call (in this case the End If line). If you step out of the procedure, the OverBudget procedure runs, your code returns to the GoShopping procedure and completes the procedure. If, however, you use the F8 key to step through your code until you reach the first line of the OverBudget procedure, stepping out of the procedure returns you to the line after the OverBudget call (the End If line). Use the following table as a cheat sheet and create some simple procedures to test the various debugging techniques shown in this chapter.

Debugging Technique


Shortcut Key

Step Into

Executes the next line of code in your procedure.


Step Over

Executes code one line at a time within the current procedure. If a second procedure is called from within the first, the entire second procedure is executed at once.


Step Out

VBA executes the entire current procedure. If executed within the second procedure, the entire second procedure is executed and execution returns to the line following the line in the first procedure that called the second procedure.


Was this article helpful?

0 0

Post a comment