Microsoft KB Archive/51309

{| = Excel: Average Number of Years, Months, and Days Between Dates =
 * width="100%"|

Last reviewed: June 27, 1997

Article ID: Q51309

SUMMARY
The following Microsoft Excel function macro calculates the average number of years, months, and days between a list of start and end dates.

To use this macro, enter the following macro commands into an Excel macro sheet:

+---++ |  |                                A                               | +---++ | 1 | aveymd                                                        | +---++ | 2 | =ARGUMENT(&quot;start&quot;,64)                                         | +---++ | 3 | =ARGUMENT(&quot;end&quot;,64)                                           | +---++ |  | =ROUND(AVERAGE(YEAR(end)-YEAR(start)-IF((MONTH(end)            | | 4 | <MONTH(start))+(MONTH(end)=MONTH(start))*(DAY(end)            | |  |  <DAY(start)),1)),1)&&quot;y &quot;                                      | +---++ | 5 | =ROUND(AVERAGE(MONTH(end)-MONTH(start)+(MONTH(end)            | |   |  <MONTH(start))*12-(DAY(end)<DAY(start))),0)&&quot;m &quot;              | +---++ | 6 | =ROUND(AVERAGE(end-DATE(YEAR(end),MONTH(end)-(DAY(end)        | |  |  <DAY(start)),DAY(start))),0)&&quot;d&quot;                              | +---++ | 7 | =RETURN(A4&A5&A6)                                             | +---++ Note: The formulas in A4, A5, and A6 must be &quot;COMMAND+ENTERed&quot; (instead of pressing RETURN on the main keyboard after typing the formula, hold down the COMMAND key and press the ENTER key on the numeric keypad). Define the commands as a function macro:


 * 1) Select cell A1 (the cell containing &quot;aveymd&quot;).
 * 2) From the Formula menu, choose Define Name.
 * 3) Under Macro, select Function.
 * 4) Click OK.

To use the macro, choose Paste Function from the Formula menu and select the macro from the list (it will appear close to or at the bottom). Type in the cell references of the start and end date arrays, respectively, separated by a comma. For example, if the start dates are in cells A2:A10 and the end dates are in cells B2:B10, the formula is

=Macro1!aveymd(A2:A10,B2:B10) where &quot;Macro1&quot; is the name of the macro sheet containing the macro.
 * }