Message Box for wrong date entered

Sam W

New Member
Joined
Feb 18, 2002
Messages
26
I need a message box to appear to let the user know that if the date that the user is entering is more than 12 months from the last date entered.
There will be two cells used for date on the worksheet. One cell will be a previous date and the other cell will be the date being entered.
I need a Macro that will look at the previous date and compare it to the date being entered. If the date being entered is in excess of 12 months a pop up window will let the user know.

Thanks, Sam
 

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.
Hi Sam

Copy this code.
Right mouse select the sheet tab and select View Code.
Paste the code in the large white expanse.
Use Alt+F11 to return to your worksheet.
A1 contains first date
B1 contains second date entry
Message will appear if 2nd date entry exceeds first by 365 days.
Change cell references to the cell locations you wish to use for your dates.

Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = "$B$1" Then
If Range("B1").Value - Range("A1").Value > 365 Then
MsgBox "Date entered is more than 12 months since previous date"
End If
End If
End Sub

You might also try using Data Validation on B1 to force use of correct date.

Hope this helps
Derek
 
Upvote 0
You can also use the Datediff function, useful if you want other periods of time to be tested (quarters, months, etc.): -

Private Sub Worksheet_Change(ByVal Target As Excel.Range)

If Target.Count > 1 Then Exit Sub
If Target.Column<> 2 Then Exit Sub
If DateDiff("d", Target.Offset(0, -1), Target.Value) > 365 Then MsgBox ""

End Sub
This message was edited by Mudface on 2002-02-21 06:25
 
Upvote 0

Forum statistics

Threads
1,213,583
Messages
6,114,490
Members
448,575
Latest member
hycrow

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