Break Mode For Fixing Things that Are Broken

The prime time for catching bugs is in Break mode. Break mode is a special mode of operation in the VBE that allows you to execute lines of code one at a time. You can watch your code execute and stop after each line to examine the state and values of your variables.

This can be a big help in eradicating logical errors, because many logical errors stem from a fundamental difference between the way you thought a certain section of code would execute and the way that VBA actually executes it. By watching critical sections of code execute line by line, many times you can quickly see the error of your ways.

You can enter Break mode in multiple ways. Listing 4.2 contains a simple procedure that you can use to experiment with Break mode.

Listing 4.2: Simple Procedure for Experimenting with Break Mode

Sub PracticeWithBreakMode() Dim n As Integer

Stop End If

Next

Next End Sub

Sub TrivialSub(x As Integer) Debug.Print "x = " & x Debug.Print "2x = " & x * 2 End Sub

This listing just prints some numbers to the immediate window. It does contain a statement you haven't seen yet, however— the Stop statement. You can see this statement in the first For.. .Next loop when n = 5. When a Stop statement is encountered, VBA suspends program execution and enters break mode. To see what happens when this occurs, follow these steps:

1. Enter the PracticeWithBreakMode procedure and the TrivialSub procedure into a module.

2. Switch back to Excel and Press ALT+F8 to display a list of executable procedures.

3. Run the PracticeWithBreakMode procedure.

If you follow these instructions, you'll probably still have the VBE window open, but minimized, when you execute the procedure. If the VBE window is closed when a Stop statement is encountered, VBA opens the VBE automatically.

WARNING f you use Stop statements, be sure to remove them before you distribute your application.

Figure 4.1 displays a screenshot of the PracticeWithBreakMode procedure in Break mode. Note the small arrow in the margin indicator bar. The margin indicator bar is on by default, but you can turn it on or off on the Editor Format tab of the Options dialog box (Tools ^ Options).

In this figure, you'll also notice that the Stop statement is highlighted (the color of the highlighting is configurable on the Editor Format tab). The highlighting and small arrow in the margin indicator bar indicate the point or the line in your program at which the execution has been suspended. Finally, if you select the Debug menu item, you'll notice that many of the items are enabled.

Once in Break mode, you can execute each line of code individually, step into or over any procedures that the current procedure calls, inspect or change variable values, move the execution point to any valid statement in the current procedure, and even modify certain aspects of your program and then run or rerun them.

Figure 4.1

Break mode in the VBE looks only slightly different than normal mode.

Figure 4.1

Break mode in the VBE looks only slightly different than normal mode.

Directing Program Execution in Break Mode

The most common way to execute code in Break mode is to step through it line by line. You can achieve this by pressing F8 or Debug ^ Step Into.

Debug i Run Tools £u

dd-Ins Window

Compte VBAProject

Step Into

F8

H

5tep Ov&

5hift+F6

¿a

Step OJt

Ctrl+-Shift+F8

-'s

Rlti To Cursor

Ctrl+FS

Add Watch...

Watch.-

Ctrh-W

Watch,,,

Shlft+F9

a

Joggle Breakpoint

F9

Clear AH Breakpoints

Ctrh-5hift+F9

Set yext Statement

Ctil+F9

«

Show Next Statement

You'll probably get tired of using the menu item though, so I'd stick with F8. The Step Into method of executing code will go into any procedure that it encounters. For example, in Listing 4.2, the second For.. .Next loop makes a call to TrivialSub. If you step through this procedure using F8, every time TrivialSub is encountered you'll step through it as well.

Many times you won't want to step through every single procedure. Case in point—after you have stepped through TrivialSub once, is there really any need to step through it the remaining four times? Or you may know that many procedures in your program have been thoroughly tested and proven and are most likely not the source of your problem. In these instances, you'll want to use Step Over— SHIFT+F8 or Debug ^ Step Over. Step Over runs any procedures called by the current procedure at normal speed, which means that your focus stays on the current procedure. If you experiment with stepping through Listing 4.2 using both F8 and SHIFT+F8, the difference between Step Into and Step Over will become clear.

If you accidentally step into a procedure, you can quickly exit it and return to the previous procedure by pressing CTRL+SHIFT+F8 or Debug > Step Out.

TIP To execute code in slow motion but without repetitively striking the keys, just hold down F8 or SHIFT+F8.

While in Break mode, you may want to rapidly advance the code to another statement of interest. Let's say that after you encounter the Stop statement in Listing 4.2, you want to execute all of the code up to the first encounter of TrivialSub. You can accomplish this by placing the cursor on the line containing TrivialSub and selecting Debug ^ Run to Cursor. When you do this, program execution resumes at normal speed and reenters Break Mode when the line containing the cursor is reached.

Let me tell you about one more way to direct program execution in Break mode. You can also change the execution point by using the Set Next Statement feature. Using Set Next Statement, you can change the execution point to any other valid statement (not comment lines or variable declarations) in the same procedure. Set Next Statement is handy because you can use it to rewind and then rerun sections of interest without starting the program over.

Set Next Statement is a menu item under the Debug menu. Alternatively, you can press CTRL+F9 to set the next statement to the line containing the cursor. Finally, you can drag and drop the execution point indicator in the margin indicator bar as shown in Figure 4.2.

Finally, to exit Break mode and resume normal program execution, press F5 or choose Run ^ Run Sub/UserForm.

Figure 4.2

You can arbitrarily run any statement in a procedure by dragging and dropping the execution point indicator. Releasing the mouse in this picture would move the execution point to the Debug.Print statement.

Alternative Methods for Entering Break Mode

Besides using Stop, which though effective, is not the best choice for most situations in which you want to enter Break mode, one of the easiest ways to enter Break mode is to press F8 in the VBE with the cursor inside an executable procedure. An executable procedure is any subroutine in a standard module that does not require any input parameters.

Another way to enter Break mode is by placing Breakpoints in your code. A Breakpoint works similarly to a Stop statement. When you execute a procedure containing a Breakpoint, your code runs normally until it reaches the statement containing the Breakpoint. At this point, Break mode is entered and program execution is suspended. Unlike using the Stop statement, you don't have to add any special statements to your code for this to work, so you don't have to worry about removing them before you distribute the application. Finally, Stop statements are persistent whereas Breakpoints are not. If you close a workbook that contains a Stop statement, the Stop statement is saved just like any other VBA statement. Breakpoints, meanwhile, are only remembered as long as the workbook is open. If you save, close, and then reopen the workbook, any Breakpoints you had before you closed the workbook are forgotten.

You can set Breakpoints in your code by placing the cursor on the line from which you would like to enter Break mode and pressing F9 or Debug ^ Toggle Breakpoint. Press F9 again to turn the Breakpoint off. You can set any number of Breakpoints in your project. To clear all of the Breakpoints you have set at once, select Debug ^ Clear All Breakpoints.

0 0

Post a comment