Print instead of MsgBox so our
code can run without interruption and still show us the variable values as the code runs.
CHAPTER 7 n DEBUGGING AND ERROR HANDLING 258
1. On Standard Module1, replace each instance of MsgBox with Debug.Print. The code
should look like Listing 7-4.
Listing 7-4. BirthYear Function Using the Debug.Print Method
Function BirthYear(Age As Integer, HadBDay As Boolean)
Dim iReturn As Integer
Dim iCurrYear As Integer
iCurrYear = Year(Date)
iReturn = iCurrYear - Age
Debug.Print "Current Year: " & CStr(iCurrYear)
Debug.Print "Birth Year before If: " & CStr(iReturn)
If Not HadBDay Then
iReturn = iReturn - 1
End If
Debug.Print "Birth Year after If: " & CStr(iReturn)
BirthYear = iReturn
End Function
2. In the Immediate window, run BirthYear, passing in False to the HadBday argument, by
typing the following:
?birthyear(30, False)
3. Press Enter to run the code. The output to the Immediate window is shown in
Figure 7-12.
Figure 7-12. Output of BirthYear function sent to Immediate window
CHAPTER 7 n DEBUGGING AND ERROR HANDLING 259
We see the three messages we were looking for, plus the bottom line shows us the return
of the function. Here we see that our code did fall into the If...End If statement where it
subtracted one from the result because the birthday did not yet occur this year.
nTip Message boxes are easy to add, but don??™t forget to remove (or comment) them when your code goes
into production. Otherwise, you??™ll have some very confused users.
Pages:
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248