Using the immediate Window in Break Mode

Once the procedure execution is suspended, when the Code window appears, you can activate the Immediate window and type VBA instructions to find out, for instance, which cell is currently active or the name of the active sheet. You can also use the Immediate window to change the contents of variables in order to correct values that may be causing errors. By now, you should be an expert when it comes to working in the Immediate window. Figure 13-6 shows the suspended ChangeCode procedure and the Immediate window with the questions that were asked of Visual Basic while in the break mode.

Figure 13-6: When code execution is suspended, you can find answers to many questions by entering appropriate statements in the Immediate window.

In break mode, you can quickly find out the contents of the variable at the cursor in the Code window. Simply hold the mouse pointer over any variable in a running procedure to find out the value of that variable. For example, in the VarValue procedure shown in Figure 13-7, the breakpoint has been set on the second occurrence of the Workbooks.Add statement.

•¡a Microsoft Visual Oos-ic - Chflp13.*li [break] - |Rtcak$ (Cade}]

Ble Edfc ^iew Insert Format Çstiug (£un IooJs âdd-lns dow

sifiJil

:|1 ¿S- B )i . S 1

KWbII il'ÉÎ-.S I® 'b&ai

_J

In; a fc, ~ œ ™ S

A % % t..

0 Oebuqqinq (Chapl3.nb)

B Microsoft Excd Objects Sheatl (5heeU) @Sh«t2(Sh«t2) " 5he=t3 (5heet3) ThisWorkbcHk Modules Breaks ffl ^ Personal (PCRSONAL.XLS) VBA Project (Book2)

|(GtiiiHial>

Sub VarValue (]

Dira îïtyNarae As String VoeJcbooks. Add ititKqkîs ■ Active Workbook. Naire |B<|[ Rboolcs- Juitî

Workbooks (triyNanie | . Activate End 3ufa |rnjilslafne = 'Book2"|_

45 ti

0 Oebuqqinq (Chapl3.nb)

B Microsoft Excd Objects Sheatl (5heeU) @Sh«t2(Sh«t2) " 5he=t3 (5heet3) ThisWorkbcHk Modules Breaks ffl ^ Personal (PCRSONAL.XLS) VBA Project (Book2)

|(GtiiiHial>

Sub VarValue (]

Dira îïtyNarae As String VoeJcbooks. Add ititKqkîs ■ Active Workbook. Naire |B<|[ Rboolcs- Juitî

Workbooks (triyNanie | . Activate End 3ufa |rnjilslafne = 'Book2"|_

Sub StopExompleI )

Dim eurCell As Range Dira nuit àe Integer

Figure 13-7: In break mode, you can find out the value of a variable by resting a mouse pointer on that variable.

When Visual Basic encounters this statement, the Code window (break mode) appears. Because Visual Basic has already executed the statement that stores the name of the active workbook in the variable myBook, you can quickly find out the value of this variable by resting the mouse pointer over its name. The name of the variable and its current value appear in a frame. To show the values of several variables used in a procedure at once, you should use the Locals window that is discussed later in this chapter.

Tip 13-3: Working within the Code Window in Break Mode

While in break mode, you can change code, add new statements, execute the procedure one line at a time, skip lines, set the next statement, use the Immediate window, and more. When Visual Basic is in break mode, all of the options on the Debug menu are available. You can enter break mode by pressing Esc, Ctrl+Break, or F8, or by setting a breakpoint.

While you work in break mode, if you change certain code, VBAwill prompt you to reset the project by displaying the following error message: "This action will reset your project, proceed anyway?" You can click OK to cease the program's execution and proceed editing your code or Cancel to delete the new changes and continue running the code from the point at which it was suspended. To see this error, place your procedure in break mode, and then proceed to change the variable declaration. As you press F5 to resume code execution, VBAwill prompt you to reset your project.

0 0

Post a comment