Results 1 to 10 of 22

Thread: UDF (user defined function) replacement for Excel's DATEDIF function

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #12
    Junior Member
    Join Date
    Jul 2013
    Posts
    3
    Rep Power
    0
    Hi Rick, thank you for this UDF - it is very useful. Unfortunately I am having some challenges using it to generate 'x years y months z days'-type output in Word. Because of the way the DateDiff() function in VBA works, I find that I have to do a manual work-around to count the right number of years/months/etc if the day of the month or month of the year in the second date is before the day/month in the first date. For example, DateDiff(FirstDate, SecondDate, "m"), where FirstDate = 20/02/2002 and SecondDate = 15/08/2002 returns 6, when it should only be 5.

    I've addressed this problem so that it seems to work in 99% of scenarios, but I'm still having the occasional problem where the number of years is miscounted, or where a day is dropped for some reason. The code I am using is below, and these are the results that it generates. I know this is a bit of a big one, but any assistance would be appreciated:

    FirstDate SecondDate Result Comments
    01/07/00 30/06/04 4 yrs 0 mths 0 days correct
    30/07/11 30/06/13 2 yrs 11 mths 1 days should be 1 yrs 11 mths 1 days
    15/02/02 20/08/02 0 yrs 6 mths 6 days correct
    15/02/02 20/08/03 1 yrs 6 mths 6 days correct
    20/02/02 15/08/02 0 yrs 5 mths 27 days correct
    20/02/02 15/08/03 1 yrs 5 mths 27 days correct
    20/08/01 15/02/02 0 yrs 5 mths 27 days correct
    20/08/01 15/02/04 2 yrs 5 mths 27 days correct
    01/07/00 30/06/01 1 yrs 0 mths 0 days correct
    08/07/11 08/06/13 1 yrs 11 mths 1 days correct
    30/10/96 30/08/05 8 yrs 10 mths 1 days
    [td]correct
    12/06/85 12/06/86 1 yrs 0 mths 1 days correct
    31/01/08 31/08/10 2 yrs 7 mths 0 days should be 2 yrs 7 mths 1 days
    31/01/08 31/08/09 1 yrs 7 mths 0 days should be 1 yrs 7 mths 1 days
    31/03/07 01/08/08 1 yrs 4 mths 2 days correct
    31/03/07 01/08/12 5 yrs 4 mths 2 days correct
    31/08/07 01/03/08 0 yrs 6 mths 2 days correct
    31/08/07 01/03/12 4 yrs 6 mths 2 days correct
    Last edited by Admin; 08-26-2013 at 09:32 AM. Reason: Removed the code.

Similar Threads

  1. Recommendation: Do not use the undocumented DATEDIF function
    By Rick Rothstein in forum Rick Rothstein's Corner
    Replies: 4
    Last Post: 05-16-2015, 10:06 PM
  2. How To Use Frequency Function In Excel
    By Transformer in forum Familiar with Commands and Formulas
    Replies: 1
    Last Post: 04-09-2013, 11:49 AM
  3. Creating drop-down function in excel
    By Jorrg1 in forum Excel Help
    Replies: 4
    Last Post: 01-09-2013, 01:45 PM
  4. Nested Search Function Excel Formula
    By trankim in forum Excel Help
    Replies: 6
    Last Post: 10-29-2012, 10:29 PM
  5. Excel Nested IF Function With 3 Conditions
    By patsir in forum Excel Help
    Replies: 3
    Last Post: 08-25-2012, 07:15 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •