Using the Locals Window and the Call Stack Dialog

If during the execution of a VBA procedure you want to keep an eye on all the declared variables and their current values, make sure to choose View | Locals Window before you run the procedure. While in break mode, Visual Basic will display a list of variables and their corresponding values in the Locals window (Figure 13-12).

The Locals window contains three columns. The Expression column displays the names of variables that are declared in the current procedure. The first row displays the name of the module preceded by the plus sign. When you click the plus sign, you can check if any variables have been declared at the module level. The class module will show here the system variable Me. In the Locals window, the global variables and variables used by other projects aren't displayed.

The second column shows the current values of variables. In this column, you can change the value of a variable by clicking it and typing the

Figure 13-12: The Locals window displays the current values of all the declared variables in the current VBA procedure.

new value. After changing the value, press Enter to register the change. You can also press Tab, Shift+Tab, or the up or down arrow or click anywhere within the Locals window after you've changed the variable value. The third column displays the type of each declared variable.

To observe the values of variables in the Locals window, do the following:

1. Choose View | Locals Window.

2. Click anywhere inside the WhatDate procedure and press F8. By pressing F8, you place the procedure in break mode. The Locals window displays the name of the current module and the local variables and their beginning values.

3. Press F8 a few more times while keeping an eye on the Locals window.

4. Press F5 to continue running the procedure.

The Locals window also contains a button with three dots. This button opens the Call Stack dialog box (Figure 13-13), which displays a list of all active procedure calls. An active procedure call is a procedure that is started but not completed. You can also activate the Call Stack dialog box by choosing View | Call Stack. This option is only available in break mode.

Figure 13-13: The Call Stack dialog box displays a list of the procedures that are started but not completed.

The Call Stack dialog box is especially helpful for tracing nested procedures. Recall that a nested procedure is a procedure that is being called from within another procedure. If a procedure calls another, the name of the called procedure is automatically added to the Calls list in the Call Stack dialog box. When Visual Basic has finished executing the statements of the called procedure, the procedure name is automatically removed from the Call Stack dialog box. You can use the Show button in the Call Stack dialog box to display the statement that calls the next procedure listed in the Call Stack dialog box.

0 0

Post a comment