VBA code to check if the value of a cell is more than 2 months ahead of today

mecerrato

Board Regular
Joined
Oct 5, 2015
Messages
174
Office Version
  1. 365
Platform
  1. Windows
I am using the change event to test if a date entered into cell G17 is more than 2 months ahead but don't know how to finish the code

This is what I have
VBA Code:
If Sheets("Refi").Range("G17").Value > Now() Then MsgBox "This date is more than 2 months ahead, please correct", vbCritical

Can someone tell me what to add to the Now() part?
 

Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce
How about
VBA Code:
If Sheets("Refi").Range("G17").Value > DateAdd("m", 2, Date) Then MsgBox "This date is more than 2 months ahead, please correct", vbCritical
 
Upvote 0
How about
VBA Code:
If Sheets("Refi").Range("G17").Value > DateAdd("m", 2, Date) Then MsgBox "This date is more than 2 months ahead, please correct", vbCritical
This works, I modified my code where I want to make the date to today's date ONLY if the date entered is more than 2 months old
if the date is not more than 2 months old I want it to leave date entered by user.
But the code below is changing the date no matter what, can you tell me what is wrong?

VBA Code:
If Target.Address = "$G$18" Then 
        Application.EnableEvents = False
        If Sheets("Refi").Range("G18").Value > DateAdd("m", 2, Date) Then MsgBox "This date is more than 2 months ahead, please correct", vbCritical
        Sheets("Refi").Range("G18").Value = Now()
        Else
        Range("G18").Select
        Application.EnableEvents = True
End If
 
Upvote 0
What is the point of showing a message box telling the user to correct the date, if the code then does just that?
 
Upvote 0
What is the point of showing a message box telling the user to correct the date, if the code then does just that?
I am not correcting the date I am simply entering today's date to avoid other errors on the sheet that depend on that field.
The issue is that it changes the date even if the date is correct (meaning less than 2 months). If the date is less than 2 months I want the code to accept the users input and not change it. I only want it to change it if is more than 2 months.
 
Upvote 0
You have a message box that informs the user the date is wrong & telling them to correct, but you then want to change the date anyway. Do you need the message box?
 
Upvote 0
You have a message box that informs the user the date is wrong & telling them to correct, but you then want to change the date anyway. Do you need the message box?
I am not correcting the date I am simply entering today's date to avoid other errors on the sheet that depend on that field IF they enter a date that is older than 2 months.
IF they enter a date that is within 2 months I don't what the code to do anything, this is the part that is not working for me.
 
Upvote 0
You have a message box that says
VBA Code:
"This date is more than 2 months ahead, please correct",
if the date is more than 2 months out. But you are then trying to change the date. Why tell the user to correct the date if the code is going to do that?
 
Upvote 0
You have a message box that says
VBA Code:
"This date is more than 2 months ahead, please correct",
if the date is more than 2 months out. But you are then trying to change the date. Why tell the user to correct the date if the code is going to do that?
I am not trying to change the date, I am ONLY temporarily changing the date to avoid other errors. The date I need the user to enter has to be within months. IF they enter a date greater than 2 months I get errors on other parts of the sheet so I am temporarily changing it to today's date.
IF they enter a date within 2 months I don't want the code to change it to today's date.
 
Upvote 0
Yes I know what you are trying to do. What I am pointing out is you are telling the user to correct the date if it's more than 2 months out, but then changing it with code. This is going to confuse the user,
who having been told to correct the date they look at it, it doesn't need changing.
 
Upvote 0

Forum statistics

Threads
1,214,904
Messages
6,122,169
Members
449,070
Latest member
webster33

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