30.04.2017 Views

4523756273

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

202<br />

Part III: Programming Concepts<br />

After you’ve debugged your code, be sure to remove all the Debug.Print<br />

statements. Even big companies like Microsoft occasionally forget to remove<br />

their Debug.Print statements. In several previous versions of Excel, every<br />

time the Analysis ToolPak add-in was opened, you’d see several strange messages<br />

in the Immediate window (as shown in Figure 13-4). That problem was<br />

fixed in Excel 2007.<br />

Figure 13-4:<br />

Even professional<br />

programmers<br />

sometimes<br />

forget to<br />

remove their<br />

Debug.Print<br />

statements.<br />

Using the VBA debugger<br />

The Excel designers are intimately familiar with the concept of bugs. consequently,<br />

Excel includes a set of debugging tools that can help you correct problems<br />

in your VBA code. The VBA debugger is the topic of the next section.<br />

About the Debugger<br />

In this section, I discuss the gory details of using the Excel debugging tools.<br />

These tools are much more powerful than the techniques I discuss in the previous<br />

section. But along with power comes responsibility. Using the debugging<br />

tools takes a bit of setup work.<br />

Setting breakpoints in your code<br />

Earlier in this chapter, I discuss using MsgBox functions in your code to monitor<br />

the values of certain variables. Displaying a message box essentially halts<br />

your code in mid-execution, and clicking the OK button resumes execution.<br />

Wouldn’t it be nice if you could halt a routine’s execution, take a look at the<br />

value of any of your variables, and then continue execution? Well, that’s<br />

exactly what you can do by setting a breakpoint. You can set a breakpoint in<br />

your VBA code in several ways:

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!