Stepping Through Code

In most cases, you design code to run with little or no user intervention. However, when you're testing code, sometimes it is helpful to do more than insert a couple of breakpoints or include some Debug.Print statements. If you're running code with several 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 mistakes in the logic of the code.

To step through your code, place the cursor at the point that you want to initiate the process and 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 press F8 to begin code execution, the name of the sub or function is highlighted in yellow. Subsequent presses 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.

Stepping through code is an important tool so it is worth reiterating how the process works. The first instance of F8 highlights the next executable code; the subsequent instance of F8 executes the highlighted code. If nothing is highlighted, F8 highlights code; if something is highlighted, F8 runs it.

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, you can execute the entire called procedure and then return to line-by-line execution of the calling procedure. This is called stepping over the procedure, and it is done by pressing Shift+F8. Stepping over the called procedure executes the entire procedure and then returns to the calling procedure, to proceed with code execution one step at a time. If you're within a called procedure, you can press Ctrl+Shift+F8 to step out of the current procedure. What's the difference between stepping over and stepping out of the procedure? If you're already in the called procedure, the two are exactly the same. But here's an example that illustrates the difference and gives you some practice with code. Assume you're stepping through the following code (which is in the Chapter 4 download material):

Option Compare Database

Private blnUnderBudget As Boolean

Const curBudget = 1000

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

"Suits")

For i=1To intSuits

curTotalPrice = curTotalPrice + curSuitPrice

If curTotalPrice > curBudget Then

blnUnderBudget = False

Else

blnUnderBudget = True

End If

Next

If blnUnderBudget = False Then

OverBudget

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, and 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

Description

Keyboard Shortcut

Step Into

Executes the next line of code in your procedure (highlights line in yellow).

F8

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.

Shift+F8

Step Out

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

Ctrl+Shift+F8

Was this article helpful?

0 0

Responses

  • furio
    What is the difference between stepping into stepping over and stepping out in vba?
    8 years ago

Post a comment