Saturday, October 27, 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

No comments:

Post a Comment

Hi User, Thank You for visiting My Blog. Please post your genuine Feedback or comments only related to this Blog Posts. Please do not post any Spam comments or Advertising kind of comments which will be Ignored.

Featured Post from this Blog

How to compare Current Snapshot Data with Previous Snapshot in Power BI

How to Dynamically compare two Snapshots Data in Power BI Scenario: Suppose, we have a sample Sales data, which is stored with Monthly Snaps...

Popular Posts from this Blog