564 attributed reviews in the last 3 years
Refreshingly small course sizes
Outstandingly good courseware
Whizzy online classrooms
Wise Owls only (no freelancers)
Almost no cancellations
We have genuine integrity
We invoice after training
Review 30+ years of Wise Owl
View our top 100 clients
Normally, you want your code to run as quickly as possible. Occasionally, however, it's useful to run your code line-by-line at a much slower pace. This allows you to check that your procedure behaves as expected when you run it. This process is referred to as stepping through code and is one of several debugging techniques you'll use to help fix problems in your procedures.
Stepping through code allows you to run it at your own pace and watch what happens in Excel as you do so.
You can click here to download the sample code used in this part of the lesson.
The completed code is the same as the code you can find in the Files Needed section above.
Before beginning to debug a procedure, it can be useful to display the Debug toolbar. To do this, choose View | Toolbars | Debug from the menu.
This toolbar contains several useful options to help find problems in your code.
To begin stepping through a procedure, first click somewhere within it:
Click anywhere between Sub and End Sub to select the procedure you want to step through.
Now choose one of the options from the table below:
|Debug | Step Into||F8|
When you do this, assuming that your procedure doesn't contain any syntax or compile errors, the first line of the subroutine will be highlighted in yellow:
This is how the procedure should look at this point.
At this point your procedure is in break mode, which means that it is paused and waiting for you to tell it how to proceed. The simplest thing you can do at this point is to execute the line which is currently highlighted in yellow. You can do this by choosing an option from the table below:
|Debug | Step Into||F8|
When you do this, the currently highlighted line will be executed and the next line of code becomes highlighted:
Remember: the currently highlighted line hasn't been executed yet.
You can continue this process until you reach the end of the procedure. It can be helpful to arrange the screen so that you can see both the Excel and VBE windows side-by-side:
Arrange the windows so that you can see both your code and the workbook at the same time.
When you have your screen arranged this way you can see the effect each instruction has as you execute them one-by-one, as shown in the animation below:
Continue pressing F8 to execute each line in turn.
It's important to execute the End Sub line to ensure that you exit break mode.
You can press F5 to quickly run the remaining instructions in the procedure at any point.
You don't have to step through an entire procedure to reach the end; you can stop your subroutine at any point by choosing to reset it. To do this, either choose Run | Reset from the menu, or click the toolbar button shown in the diagram below:
Click this button to stop your procedure at any time.
When you're stepping through a procedure you don't have to follow your code in the order you've written it. You can change which instruction will be executed next by dragging the yellow arrow to different lines of code:
In this example the third line of code is currently the active instruction. We can drag the yellow arrow on the left of the screen to any other line to make it the active instruction. Here we're moving back to the previous line of code.
You can make changes to a line of code before executing it again:
Editing a line of code will temporarily remove the yellow highlighting, but the yellow arrow remains visible at the left of the code window.
After editing the instruction we can resume stepping through the code by pressing F8 or choosing Debug | Step Into from the menu:
Pressing F8 executes the active instruction and moves to the next line.
You can then continue stepping through the procedure by pressing F8, run the procedure to its end by pressing F5, or stop the procedure by choosing Run | Reset from the menu.
You can continue to practise stepping through a procedure using the sample code provided. When you're comfortable, move on to the next part of the lesson to learn how to use breakpoints in your code.
25 Aytoun Street