'DateDif2 ' 'interval : "Y", "M", "D","MD","YM","YD". ' See the help topic for the DATEDIF() worksheet function. Function DateDif2(ByVal start_date As Variant, _ ByVal end_date As Variant, ByVal interval As String) As Variant Dim y As Integer, m As Integer, d As Integer Dim flag As Boolean If IsDate(start_date) And IsDate(end_date) Then start_date = CDate(start_date) end_date = CDate(end_date) flag = start_date <= end_date End If If Not flag Then DateDif2 = CVErr(xlErrNum) Exit Function End If Select Case UCase(interval) Case "Y" y = Year(end_date) - Year(start_date) m = Month(end_date) - Month(start_date) d = Day(end_date) - Day(start_date) If d < 0 Then m = m - 1 If m < 0 Then y = y - 1 DateDif2 = y Case "M" y = Year(end_date) - Year(start_date) m = Month(end_date) - Month(start_date) d = Day(end_date) - Day(start_date) If d < 0 Then m = m - 1 DateDif2 = y * 12 + m Case "D" DateDif2 = end_date - start_date Case "MD" d = Day(end_date) - Day(start_date) If d < 0 Then d = Day(end_date - Day(end_date)) - Day(start_date) If d < 0 Then d = Day(end_date) Else d = d + Day(end_date) End If DateDif2 = d Case "YM" m = Month(end_date) - Month(start_date) d = Day(end_date) - Day(start_date) If d < 0 Then m = m - 1 If m < 0 Then m = m + 12 DateDif2 = m Case "YD" If DateSerial(Year(end_date), Month(start_date), Day(start_date)) > end_date Then DateDif2 = end_date - DateSerial(Year(end_date) - 1, Month(start_date), Day(start_date)) Else DateDif2 = end_date - DateSerial(Year(end_date), Month(start_date), Day(start_date)) End If Case "YMD" y = Year(end_date) - Year(start_date) m = Month(end_date) - Month(start_date) d = Day(end_date) - Day(start_date) If d < 0 Then d = Day(end_date - Day(end_date)) - Day(start_date) If d < 0 Then d = Day(end_date) Else d = d + Day(end_date) m = m - 1 End If If m < 0 Then m = m + 12 y = y - 1 End If DateDif2 = CLng(y) * 10000 + m * 100 + d Case Else DateDif2 = CVErr(xlErrNum) End Select End Function