Results 1 to 10 of 22

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

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Senior Member
    Join Date
    Jun 2012
    Posts
    337
    Rep Power
    12
    Yes, it is/was
    So to improve readability:

    Code:
    Function xlDATEDIF(ByVal StartDate As Date, ByVal EndDate As Date, Interval As String) As Long
      If StartDate < EndDate Then
        sp = Split("Y|M|D|YY|MM|DD", "|")
        sn = Array(0, 0, 0, 0, 0, 0)
    
        sn(0) = Year(EndDate) - Year(StartDate)
        sn(1) = DateDiff("m", StartDate, EndDate)
        sn(2) = EndDate - StartDate
        sn(3) = Year(EndDate) - Year(StartDate) - IIf(100 * Month(StartDate) + Day(StartDate) > 100 * Month(EndDate) + Day(EndDate), 1, 0)
        sn(4) = DateDiff("m", StartDate, EndDate) - IIf(Day(StartDate) > Day(EndDate), 1, 0)
        sn(5) = EndDate - StartDate - IIf(Format(StartDate, ",0000") * 1 > Format(EndDate, ",0000") * 1, 1, 0)
        
        xlDATEDIF = sn(Application.Match(Interval, sp, 0) - 1)
      Else
        Err.Raise 5
      End If
    End Function
    Last edited by snb; 10-19-2012 at 02:08 PM.

  2. #2
    Junior Member
    Join Date
    May 2013
    Posts
    8
    Rep Power
    0
    Hello,

    I have a couple of problems with the code. For reference, I'm using Windows 7 and Excel XP.

    I hate to bring up dead threads, but I was looking to do a DATEDIF function for Excel so that I could subtract dates. I tried all three sets of code provided in the code boxes, but I keep getting a #VALUE! error. I'm pretty new to VBA, though I have a few years of experience in PHP, C++, and Java (and a few minutes' experience with Perl), so I can't go in and debug because I don't understand all the terms. Can one of you tell me what I'm doing wrong? Here are my cells, so you guys can replicate the problem.

    A1 = today()
    B1 = today() + b2
    B2 = 60 //I also used -60 to see if I had the dates backwards.
    C1 = xldatedif(A1,B1)

  3. #3
    Forum Guru Rick Rothstein's Avatar
    Join Date
    Feb 2012
    Posts
    659
    Rep Power
    13
    Quote Originally Posted by ProspectiveCounselor View Post
    Hello,

    I have a couple of problems with the code. For reference, I'm using Windows 7 and Excel XP.

    I hate to bring up dead threads, but I was looking to do a DATEDIF function for Excel so that I could subtract dates. I tried all three sets of code provided in the code boxes, but I keep getting a #VALUE! error. I'm pretty new to VBA, though I have a few years of experience in PHP, C++, and Java (and a few minutes' experience with Perl), so I can't go in and debug because I don't understand all the terms. Can one of you tell me what I'm doing wrong? Here are my cells, so you guys can replicate the problem.

    A1 = today()
    B1 = today() + b2
    B2 = 60 //I also used -60 to see if I had the dates backwards.
    C1 = xldatedif(A1,B1)
    You are missing the required 3rd argument... the code letter(s) for the interval whose date difference you want calculated. For example, to get the number of days between the dates in A1 and B1, you formula in C1 would need to look like this (what I highlighted in red is what you forgot to include)...

    C1 = xldatedif(A1,B1,"d")

    I am presuming since you are looking for the replacement to Excel's DATEDIF function, that you already know all the interval code letters.
    Last edited by Rick Rothstein; 05-05-2013 at 08:11 PM.

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
  •