Testing VBA Procedures

So far in this book you've created and executed dozens of sample procedures and functions. Because most of these procedures were quite short, finding errors wasn't very difficult. However, when writing longer and more complex procedures, locating the source of errors is more tedious and time-consuming. Fortunately, Visual Basic Editor provides a set of handy tools that can make the process of tracking down your VBA problems easier, faster, and less frustrating. Bugs are errors in computer programs and debugging is a process of locating and fixing those errors. Debugging allows you to find out the reason your procedure doesn't work the way it's supposed to work. You can do this by stepping through the code of your procedure or checking the values of variables.

When testing your VBA procedure, use the following guidelines:

■ If you want to analyze your procedure, step through your code one line at a time by pressing F8, or choose Debug | Step Into.

■ If you suspect that an error may occur in a specific place in your procedure, use a breakpoint.

■ If you want to monitor the value of a particular variable or expression used by your procedure, add a watch expression.

■ If you are tired of scrolling through a long procedure to get to sections of code that interest you, set up a bookmark to jump quickly to the desired location.

Each of these guidelines is demonstrated in this chapter in a hands-on example.

0 0

Post a comment