Freedom Computers

Datediff Calculate difference between two dates. Date difference. Subtract a date

Datediff Calculate difference between two dates. Date difference. Subtract a date

Sub auto_open()
'This macro can be cut and pasted into an empty worksheet macro.
Dim msg, startdate, enddate, period, temp
temp = 0

startdate = InputBox("Enter the starting date")
enddate = InputBox("Enter the Ending date")
period = InputBox("Enter period for Calculation D, W, M or Y")

Select Case period
Case Is = "D"
msg = DateDiff("d", startdate, enddate) & " Days between dates"
Case Is = "d"
msg = DateDiff("d", startdate, enddate) & " Days between dates"

Case Is = "W"
temp = DateDiff("d", startdate, enddate) / 7
temp = (Int((temp * 100) + 0.5)) / 100
msg = temp & " Weeks between dates"

Case Is = "w"
temp = DateDiff("d", startdate, enddate) / 7
temp = (Int((temp * 100) + 0.5)) / 100
msg = temp & " Weeks between dates"
Case Is = "M"
msg = DateDiff("m", startdate, enddate) & " Months between dates"
Case Is = "m"
msg = DateDiff("m", startdate, enddate) & " Months between dates"

Case Is = "Y"
msg = DateDiff("yyyy", startdate, enddate) & " Years between dates"

Case Is = "y"
msg = DateDiff("yyyy", startdate, enddate) & " Years between dates"


End Select

MsgBox msg
End Sub


EXCEL