Debugging Techniques

In this section, I discuss the four most common methods for debugging Excel VBA code:

1 Examining the code

1 Inserting MsgBox functions at various locations in your code 1 Inserting Debug.Print statements 1 Using the Excel built-in debugging tools

Examining your code

Perhaps the most straightforward debugging technique is simply taking a close look at your code to see whether you can find the problem. If you're lucky, the error jumps right out and you can quickly correct it.

Notice I said, "If you're lucky." That's because often you discover errors when you have been working on your program for eight hours straight, it is 2:00 a.m., and you are running on caffeine and willpower. At times like that, you are lucky if you can even see your code, let alone find the bugs. Thus, don't be surprised if examining your code alone doesn't expunge all the bugs it contains.

Using the MsgBox function

A common problem in many programs involves one or more variables not taking on the values you expect. In such cases, monitoring the variable(s) while your code runs is a helpful debugging technique. Do this by inserting temporary MsgBox functions in your routine. For example, if you have a variable named CellCount, you can insert the following statement:

MsgBox CellCount

When you execute the routine, the MsgBox function displays CellCount's value.

If your message box shows something unexpected, hit control-break and click the debug button. Excel will take you to the VBE and highlight the next statement for you to step through.

It's often helpful to display the values of two or more variables in the message box. The following statement displays the current value of LoopIndex and CellCount, as shown in Figure 13-2:

MsgBox LoopIndex & " " & CellCount

Figure 13-2:

Using a message box to display the value of two variables.

Notice that I combine the two variables with the concatenation operator (&) and insert a space character between them. Otherwise, the message box strings the two values together, making them look like a single value. You can also use the built-in constant, vbNewLine, in place of the space character. vbNewLine inserts a line-feed break, which displays the text on a new line. The following statement displays three variables, each on a separate line:

MsgBox LoopIndex & vbNewLine & CellCount & vbNewLine & MyVal

Microsoft Excel

1 72

This technique isn't limited to monitoring variables. You can use a message box to display all sorts of useful information while your code is running. For example, if your code loops through a series of sheets, the following statement displays the name and type of the active sheet:

MsgBox ActiveSheet.Name &

& TypeName(ActiveSheet)

I use MsgBox functions frequently when I debug my code. Just make sure that you remove them after you identify and correct the problem.

Inserting Debug.Print statements

As an alternative to using MsgBox functions in your code, you can insert one or more temporary Debug.Print statements. Use these statements to print the value of one or more variables in the Immediate window. Here's an example that displays the value of three variables:

Debug.Print LoopIndex, CellCount, MyVal

Notice that the variables are separated with a comma. You can display as many variables as you like with a single Debug.Print statement. If VBE's Immediate window is not visible, press Ctrl+G.

After you've debugged your code, make sure to remove all the Debug.Print statements. Even big companies like Microsoft occasionally forget to remove their Debug.Print statements. In several previous versions of Excel, every time the Analysis ToolPak add-in was opened, you'd see several strange messages in the Immediate window (as shown in Figure 13-3). That problem was fixed in Excel 2007.

Figure 13-3:

Even professional programmers sometimes forget to remove their Debug.Print statements.

Figure 13-3:

Even professional programmers sometimes forget to remove their Debug.Print statements.

Using the VBA debugger

The Excel designers are intimately familiar with the concept of bugs. Consequently, Excel includes a set of debugging tools that can help you correct problems in your VBA code. The VBA debugger is the topic of the next section.

0 0

Post a comment