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.

Chapter 12<br />

Error-Handling Techniques<br />

In This Chapter<br />

▶ Understanding the difference between programming errors and run-time errors<br />

▶ Trapping and handling run-time errors<br />

▶ Using the VBA on Error and Resume statements<br />

▶ Finding out how you can use an error to your advantage<br />

To err is human. To anticipate errors is divine. When working with VBA,<br />

you should be aware of two broad classes of errors: programming errors<br />

and run-time errors. (I cover programming errors, also known as bugs, in<br />

Chapter 13.) A well-written program handles errors the way Fred Astaire<br />

danced: gracefully. Fortunately, VBA includes several tools to help you identify<br />

errors — and then handle them gracefully.<br />

Types of Errors<br />

If you’ve tried any of the examples in this book, you have probably encountered<br />

one or more error messages. Some of these errors result from bad VBA<br />

code. For example, you may spell a keyword incorrectly or type a statement<br />

with the wrong syntax. If you make such an error, you won’t even be able to<br />

execute the procedure until you correct it.<br />

This chapter does not deal with those types of errors. Instead, I discuss runtime<br />

errors — the errors that occur while Excel executes your VBA code.<br />

More specifically, this chapter covers the following fascinating topics:<br />

✓ Identifying errors<br />

✓ Doing something about the errors that occur<br />

✓ Recovering from errors<br />

✓ Creating intentional errors (Yes, sometimes an error can be a good thing.)

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

Saved successfully!

Ooh no, something went wrong!