VBA Help! Displaying date differences

sliollio

New Member
Joined
Sep 8, 2014
Messages
22
Trying to write a macro for an assignment that will let you enter a date, display the year, month, and day in seperate msg boxes, then display if the date entered is from the future/past and compute the difference based on the current date.

Not sure how to get it to calculate the difference between the dates or the future/past part. I tried and If, then, else statement but it would only say "past" regardless of what date was put in.

Any help is greatly appreciated!:)

Sub macro1()
Dim TheDate As Variant
TheDate = InputBox("Insert: Date (mm/dd/yyyy)")
MsgBox Format(TheDate, "mmmm")
MsgBox Format(TheDate, "dd")
MsgBox Format(TheDate, "yyyy")

End Sub
 

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
Just an idea, you can modify further
Code:
Sub macro1()
    Dim TheDate As Date
    TheDate = InputBox("Insert: Date (mm/dd/yyyy)")
    Select Case TheDate - Date
        Case Is < 0
            MsgBox "Date is in the past"
        Case 0
            MsgBox "Date is the current date"
        Case Is > 0
            MsgBox "Date is in the future"
    End Select
    MsgBox Format(TheDate, "mmmm")
    MsgBox Format(TheDate, "dd")
    MsgBox Format(TheDate, "yyyy")
End Sub
 
Upvote 0
Thank you so much! Works perfectly! Do you know if there is a way that within that same MsgBox that displays "past/present/future" that you can display the calculated difference in days? I tried doing some DateDiff functions but can't get it to run. Much less within that same MsgBox
 
Upvote 0
Note that i used days all through, but if the difference is just 1 day, that should be singular, you will need an if statement for that or add more cases
Code:
Sub macro1()
    Dim TheDate As Date
    TheDate = InputBox("Insert: Date (mm/dd/yyyy)")
    Select Case TheDate - Date
        Case Is < 0
            MsgBox "Date is in the past by " & Date - TheDate & "days"
        Case 0
            MsgBox "Date is the current date"
        Case Is > 0
            MsgBox "Date is in the future " & TheDate - Date & "days"
    End Select
    MsgBox Format(TheDate, "mmmm")
    MsgBox Format(TheDate, "dd")
    MsgBox Format(TheDate, "yyyy")
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,897
Messages
6,122,148
Members
449,066
Latest member
Andyg666

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top