Step Through A Procedure

I tl ou can debug your procedure by stepping through the execution of the code one line at a time. Stepping through the code in this fashion is commonly referred to as tracing. Unlike break points, which execute the code until a break is encountered, tracing literally executes a line and waits for you to indicate that you want to execute the next line of code. This method of debugging works well for locating logical errors in your code.

When you step through your code, you can also use watches to monitor the value of different expressions. A watch is an expression for which you monitor the value. As you step through the procedure, the values of the watches update. You can quickly see at what point in the procedure your watches are valid. See the section "Using Watch Expressions to Debug a Procedure" for more information on creating a watch.

As the code executes, the values of each local variable display in the Locals window. Make sure the Locals window displays prior to selecting the option to step through the code. See Chapter 2 for more information about using the Locals window.

When you step into the current procedure, the Visual Basic Editor selects the first line of code in the procedure, the Sub or Function statement. Continue stepping through the code using the Step Into option. As you select the option, the Visual Basic Editor highlights the next line of code to execute. The Locals window updates the values of the local variables each time there is a value change. Finally, the Watches window monitors the values of any watch expressions created for the procedure.

As you step through a procedure, if a code statement calls another procedure, the Visual Basic Editor also steps through the called procedure. After that procedure executes, the control returns to the original procedure.

STEP THROUGH A PROCEDURE

STEP THROUGH A PROCEDURE

0 Click View O Watch Window.

■ The Watches and Locals windows display in the last viewed location.

LQ In the Projects window, open the module containing the procedure you want to debug.

Note: See Chapter 2 for information on opening VBA modules.

0 Click View O Watch Window.

—0 Click View O Locals Window.

■ The Watches and Locals windows display in the last viewed location.

0 0

Post a comment