The Debuggers Toolkit

The VBE gives us tools to do all of the above and more. The Debug menu, shown in Figure 7-1, contains the VBE's debugging command. The Debug toolbar (Figure 7-2) contains commonly used commands from the Debug menu.

Figure 7-1. The Debug menu

Figure 7-2. The Debug toolbar

Figure 7-2. The Debug toolbar

Table 7-1 describes the commands on the Debug menu and shows the corresponding Debug toolbar button for that command when one is available. Table 7-2 describes the remaining commands on the Debug toolbar.

Table 7-1. Descriptions of the Debug Menu and Toolbar Commands

Debug Toolbar Button

(If Applicable) Command

Shortcut Key

Description

— Compile VBA Project

Compiles the project. It does not create an executable or redistributable component, but it does check that the syntax is correct.

51 Step Into

F8

Executes code one statement at a time. The next statement displayed is the next statement in the current procedure (break mode does not flow into the called procedure or function).

Step Out

Ctrl+Shift+F8

Executes the remaining lines of a procedure or function from where the current execution point lies.

— Run to Cursor

Ctrl+F8

Executes code from the current location to the statement at the cursor location. This is useful when there's a need to avoid stepping through large loops.

Debug Toolbar Button

(If Applicable)_Command_Shortcut Key Description_

Displays the Add Watch dialog box, where you enter a watch expression (a user-defined expression that enables you to see the contents of a variable or the result of an expression). The expression can be any valid Visual Basic expression (e.g., MyVariable = "New York").

Displays the Edit Watch dialog box, in which you can edit or delete a watch expression.

Displays the Quick Watch dialog box with the current value of the selected expression.

Inserts or removes a breakpoint at the current line.

Clear All Breakpoints Ctrl+Shift+F9 Removes all breakpoints in a project.

Set Next Statement Ctrl+F9 Sets the execution point to the line of code you choose. You can set a different line of code to execute after the currently selected statement by selecting the line of code you want to execute and choosing the Set Next Statement command, or by dragging the Current Execution Line indicator to the line of code you want to execute.

Show Next Statement — Highlights the next statement to be executed.

— Edit Watch Ctrl+W M QuickWatch Shift+F9 ^ Toggle Breakpoint Shift+F9

There are a few additional commands available to us on the Debug toolbar. Some are common commands from the Debug menu and others are commonly used items from the View menu. They are described in Table 7-2.

Table 7-2. Description of the Debug Toolbar Commands

Debug Toolbar Button Command

Shortcut Key Description

Design Mode — Turns design mode for a UserForm off or on.

Continue F5 Runs the current procedure if the cursor is in a procedure. Runs a UserForm if the UserForm is active. Opens the Macro dialog box if neither the code window nor a UserForm is active.

Break Ctrl+Break Stops execution of a procedure and switches to break mode.

Reset — Stops execution of a procedure, clears module level variables, and resets the project.

Continued

Table 7-2. Continued

Debug Toolbar Button Command

Shortcut Key Description

Locals Window

Displays the Locals window. The Locals window displays all of the variables in the current procedure and their values.

Immediate Window Ctrl+G

Displays the Immediate window. The Immediate window allows you to type or paste code and press Enter to run it.

Watch Window

Displays the Watch window. The Watch window is where you define expressions to be monitored (in the Watch window).

Call Stack

Ctrl+L

Displays the Call Stack dialog box. The Call Stack dialog box lists procedures that have started but have not finished, in the order they were called.

So there we have the tools we'll use to search out errors in our code. Before we begin putting these to work and seeing what they can do for us, though, let's take a very quick look at one line of code that the VBE will add for you that can help eliminate some coding errors from the start: Option Explicit.

0 0

Post a comment