Compare date in a cell to TODAY and have msgbox if date is in the past

Mikeymike_W

Board Regular
Joined
Feb 25, 2016
Messages
171
Hi All,

I have a cell in my sheet where the user will insert a date.
If this date is in the past i would like a msgbox to open and inform them of this.

I feel like this should be really easy but i just can get it working.

Thanks in advance for any help,

Mike
 

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.
I have a cell in my sheet where the user will insert a date.
If this date is in the past i would like a msgbox to open and inform them of this.
When you ask a question, you should try to be specific. Assuming the "cell in my sheet where the user will insert a date" is cell A1, give this event code a try...
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
  If Target.Address(0, 0) = "A1" Then
    If IsDate(Target.Value) Then
      If Target.Value < Date Then MsgBox "That date is in the past!"
      Target.Select
    End If
  End If
End Sub


HOW TO INSTALL Event Code
------------------------------------
If you are new to event code procedures, they are easy to install. To install it, right-click the name tab at the bottom of the worksheet that is to have the functionality to be provided by the event code and select "View Code" from the popup menu that appears. This will open up the code window for that worksheet. Copy/Paste the event code into that code window. That's it... the code will now operate automatically when its particular event procedure is raised by an action you take on the worksheet itself. Note... if you are using XL2007 or above, make sure you save your file as an "Excel Macro-Enabled Workbook (*.xlsm) and answer the "do you want to enable macros" question as "yes" or "OK" (depending on the button label for your version of Excel) the next time you open your workbook.
 
Last edited:
Upvote 0
Hi All,

I have a cell in my sheet where the user will insert a date.
If this date is in the past i would like a msgbox to open and inform them of this.

I feel like this should be really easy but i just can get it working.

Thanks in advance for any help,

Mike
Hi Mike,

Without knowing which cell you are wanting the users to complete I have had to make a guess at cell A1. So basically, the following Worksheet_Change macro will check cell A1 when it is updated and if it is a date AND that date has passed then it will display an error message:

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    If Not Intersect(Target, Range("A1")) Is Nothing Then
        If IsDate(Target.Value) Then
            If Now > Target.Value Then
                MsgBox "Date has already passed"
            End If
        End If
    End If
End Sub
 
Upvote 0
Hi Rick, Hi Fishboy,

Thanks very much for your respoinses and sorry for my vagueness.

I tried both pieces of code and they both seem to work as well as the other.

Many thanks for all your help,

Mike
 
Upvote 0
Hi Rick, Hi Fishboy,

Thanks very much for your respoinses and sorry for my vagueness.

I tried both pieces of code and they both seem to work as well as the other.

Many thanks for all your help,

Mike
You're welcome. Happy to help.
 
Upvote 0
Hi,

Can i ask one more thing please.

How would i alter the code so that it allows for 4 months?

So if the date entered is more than 4 months from todays date then the msgbox will appear?

Thanks in advance,

Mike
 
Upvote 0
How would i alter the code so that it allows for 4 months?

So if the date entered is more than 4 months from todays date then the msgbox will appear?
I presume you mean more than 4 months in the past from today's date. Here is my code modified to do that...
Code:
[table="width: 500"]
[tr]
	[td]Private Sub Worksheet_Change(ByVal Target As Range)
  If Target.Address(0, 0) = "A1" Then
    If IsDate(Target.Value) Then
      If Target.Value < DateAdd("m", -4, Date) Then MsgBox "That date is in the past!"
      Target.Select
    End If
  End If
End Sub[/td]
[/tr]
[/table]
 
Upvote 0

Forum statistics

Threads
1,215,063
Messages
6,122,927
Members
449,094
Latest member
teemeren

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