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 252 | Next

Jim DeMarco

"Pro Excel 2007 VBA"


1. Add a variable declaration after the declaration for temp:
Dim sErrMsg As String
The sErrMsg variable will hold the text of the message we??™ll show our users should an error
occur. The variables should now look like those in Listing 7-11.
Listing 7-11. Variable List for GetSalesTotal
Dim currReturn As Currency
Dim cell As Range
Dim temp As Currency
Dim sErrMsg As String
2. Immediately below the variable declarations, add the following line of code to enable
error handling:
On Error GoTo Err_Handle
Here is where we tell the compiler where in our code to go if an error is fired. Err_Handle is
a line label that refers to a specific point in our code. We??™ll add it in just a moment.
3. Add two blank lines between the last two lines of code in the GetSalesTotal function,
as follows:
currReturn = temp
GetSalesTotal = currReturn
4. Put your cursor in the second blank line and add the following line label:
Exit_Function:
CHAPTER 7 n DEBUGGING AND ERROR HANDLING 279
5. Insert a blank line above the End Function line and type the following:
Exit Function
The code after the loop should now look like Listing 7-12.
Listing 7-12. Exit_Function Line Label Added
currReturn = temp
Exit_Function:
GetSalesTotal = currReturn
Exit Function
End Function
So far, with the exception of enabling error handling, our code works just like it did originally.
Now let??™s write code to handle the type mismatch error.
6. Put the insertion point at the end of the Exit Function line of code and press Enter.


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