Let??™s take a look at an example in which we are not getting an expected result and we??™d
like to see what??™s happening under the covers.
CHAPTER 7 n DEBUGGING AND ERROR HANDLING 253
nNote No one is perfect! As I was writing the code for this example, I actually made the typo we are going
to troubleshoot. When I ran the code to make sure it worked (my plan was to break the code for this example),
I did not get the result I expected!
1. In the Download section for this book on the Apress web site, find the file named
DebugExample01.xlsm and open it. This file contains sample sales data, but we??™ll ignore
that for the time being.
2. Open the VBE by choosing the Visual Basic command from the Code tab of the Developer
ribbon, or by pressing Alt+F11.
3. Open Standard Module1 by double-clicking its folder in the VBA Project window, as
shown in Figure 7-4.
Figure 7-4. Opening Standard Module1
OnModule1, you??™ll find a very useful function named BirthYear, as shown in Listing 7-1.
Listing 7-1. The BirthYear Function
Function BirthYear(Age As Integer, HadBDay As Boolean)
Dim iReturn As Integer
Dim iCurrYear As Integer
iCurrYear = Year(Date)
iReturn = iCurrYear = Age
If Not HadBDay Then
iReturn = iReturn - 1
End If
BirthYear = iReturn
End Function
CHAPTER 7 n DEBUGGING AND ERROR HANDLING 254
This function takes two inputs??”an Integer containing your age and a Boolean flag denoting
whether you??™ve had your birthday yet this year??”and it returns your birth year.
Pages:
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244