Page 279 - Excel 2007 Bible
P. 279

18_044039 ch13.qxp  11/21/06  11:05 AM  Page 236
                                   Part II
                                              Working with Formulas and Functions
                                             Calculating a person’s age
                                             A person’s age indicates the number of full years that the person has been alive. The formula in the previous
                                             section (for calculating the number of years between two dates) won’t calculate this value correctly. You can
                                             use two other formulas, however, to calculate a person’s age.
                                             The following formula returns the age of the person whose date of birth you enter into cell A1. This formula
                                             uses the YEARFRAC function.
                                                  =INT(YEARFRAC(TODAY(),A1,1))
                                                       In versions prior to Excel 2007, the YEARFRAC function was available only when the Analysis
                                       NEW FEATURE
                                       NEW FEATURE
                                                       ToolPak add-in was installed. The function is now part of Excel 2007.
                                             The following formula uses the DATEDIF function to calculate an age. (See the sidebar, “Where’s the DATE-
                                             DIF Function?”)
                                                  =DATEDIF(A1,TODAY(),”Y”)
                                                           Where’s the DATEDIF Function?
                                             ne of Excel’s mysteries is the DATEDIF function. You may notice that this function does not appear in the
                                         Odrop-down function list for the Date & Time category, nor does it appear in the Insert Function dialog
                                         box. Therefore, when you use this function, you must always enter it manually.
                                         The DATEDIF function has its origins in Lotus 1-2-3, and apparently Excel provides it for compatibility pur-
                                         poses. For some reason, Microsoft wants to keep this function a secret. The function has been available since
                                         Excel 5, but Excel 2000 is the only version that ever documented it in its Help system.
                                         DATEDIF is a handy function that calculates the number of days, months, or years between two dates. The
                                         function takes three arguments: start_date, end_date, and a code that represents the time unit of interest. The
                                         following table displays valid codes for the third argument. (You must enclose the codes in quotation marks.)
                                          Unit Code       Returns
                                          “y”             The number of complete years in the period.
                                          “m”             The number of complete months in the period.
                                          “d”             The number of days in the period.
                                          “md”            The difference between the days in start_date and end_date. The months and years
                                                          of the dates are ignored.
                                          “ym”            The difference between the months in start_date and end_date. The days and years
                                                          of the dates are ignored.
                                          “yd”            The difference between the days of start_date and end_date. The years of the dates
                                                          are ignored.
                                         The start_date argument must be earlier than the end_date argument, or the function returns an error.




                                      236
   274   275   276   277   278   279   280   281   282   283   284