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. #6
    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.

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
  •