Saturday, 27 October 2012

How To Find Number of Days, Weeks, Months, Quarters Between Two Dates

Excel VBA Macro To Know Number of Days, Weeks, Months, Quarters Between Two Dates.
You can also know the Names of Days, Weeks, Months  Between Two Dates

Sub DatesDiff()
Dim X As Date 'Or You Can Declare as String
Dim Y As Date 'Or You Can Declare as String
Dim DD As String

X = "01-May-2012" 'Or You Can Declare as #01-May-2012#
Y = "01-AUG-2012" 'Or You Can Declare as #01-AUG-2012#

'To Know No.of Days Between Two Dates
DD = DateDiff("D", X, Y)
MsgBox "The No.of Days Between" & "  " & X & " and " & Y & "  " & " Are :" & DD

DD = DateDiff("Y", X, Y)
MsgBox "The No.of Days Between" & "  " & X & " and " & Y & "  " & " Are :" & DD

'To Know No.of Months Between Two Dates
DD = DateDiff("M", X, Y)
MsgBox "The No.of Months Between" & "  " & X & " and " & Y & "  " & " Are :" & DD

'To Know No.of Weeks Between Two Dates
X = "01-May-2010"
Y = "01-MAY-2012"
DD = DateDiff("WW", X, Y)
MsgBox "The No.of Weeks Between" & "  " & X & " and " & Y & "  " & " Are :" & DD

'To Know No.of Weekdays(Suppose 'Saturday'as on 01-May2010) Between Two Dates
X = "01-May-2010"
Y = "01-MAY-2012"
DD = DateDiff("W", X, Y)
MsgBox "The No.of ThisWeekDays Between" & "  " & X & " and " & Y & "  " & " Are :" & DD

'To Know No.of Years Between Two Dates
X = "01-May-2010"
Y = "01-MAY-2012"
DD = DateDiff("YYYY", X, Y)
MsgBox "The No.of Years Between" & "  " & X & " and " & Y & "  " & " Are :" & DD

'To Know No.of Quarters Between Two Dates
X = "01-May-2010"
Y = "01-MAY-2012"
DD = DateDiff("Q", X, Y)
MsgBox "The No.of Quarters Between" & "  " & X & " and " & Y & "  " & " Are :" & DD

End Sub

Thanks.,
TAMATAM
Excel VBA Macros Developer

No comments:

Post a Comment

Follow Me by Email

ExcelKingdom-Popular Posts

ExcelKingdom-Random Posts