SEARCH
0-9 A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
Prev | Current Page 251 | Next

Jim DeMarco

"Pro Excel 2007 VBA"

We??™ll begin by resetting the file as follows:
1. On Sheet1, enter N/A in cell D4.
2. If there is a totals row present, delete it.
3. Save the file.
A type mismatch error is error number 13, and occurs when you try to place a value of
one data type into an incompatible data type (in this example, a string into a numeric data
type). See Figure 7-27 in our earlier example for an example of a type mismatch error. We are
going to modify our code to trap for error 13 and display a friendly message to the user.
Open the VBE by clicking the Visual Basic command on the Code tab of the Developer ribbon,
or by pressing Alt+F11. Open Standard Module2, and find the GetSalesTotal function.
In the GetSalesTotal function, we have a loop (Figure 7-40) that uses a temporary placeholder
variable, temp, to hold the running total value of the cells in the range passed in. This
variable is defined as a Currency data type. The Currency data type can hold an awfully large
numeric value, but it cannot hold a string.
CHAPTER 7 n DEBUGGING AND ERROR HANDLING 278
Figure 7-40. The looping structure totals the cells in the temp variable.
There are a couple of things we have to do to set up a procedure for error handling:
1. Turn error handling on (also known as enabling error handling).
2. Add line labels so our code knows where to go when an error condition is fired.
3. Handle the error.
4. Resume code execution at the appropriate location.
Let??™s modify the GetSalesTotal function and add an error handler.


Pages:
239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263