I feel really silly because it seems this should be rather simple.
I'm trying to check to see if the month it is currently, is the same month it will be two days from now.
For example, Saturday is February. Monday is March. So if I run the code on Saturday, it will tell me that the months don't match.
Does this make sense?
Here's what I've tried...it's not giving me an error, but it's also not working.
If Month(Now) <> Month(Now) + 2 Then
MsgBox ("New Month")
Else
MsgBox ("Same month")
End If
VBA code for month verification
-
- Administrator
- Posts: 13005
- Joined: 16 Jan 2010, 15:49
- Location: London, Europe
Re: VBA code for month verification
You need to move your parentheses.
Instead of
Month(Now)+2
you need
Month(Now+2)
It is currently 25th February
Month(Now) is 2
Month(Now)+4 is 2+4 which of course is 6
Month(Now+4) is the month for the date 4 days in the future, which is 3
Instead of
Month(Now)+2
you need
Month(Now+2)
It is currently 25th February
Month(Now) is 2
Month(Now)+4 is 2+4 which of course is 6
Month(Now+4) is the month for the date 4 days in the future, which is 3
StuartR
-
- Lounger
- Posts: 35
- Joined: 17 Feb 2010, 22:08
Re: VBA code for month verification
Yep...I feel silly.


-
- Lounger
- Posts: 35
- Joined: 17 Feb 2010, 22:08
Re: VBA code for month verification
Somewhat related, so I'm not going to clog up the board with another thread.
I also need a check on a specific time on a date two days from now.
For example, the current date is March 1. I need to see if the date value in a specific cell is before or after 8:00 PM on March 3rd.
Here is what I'm starting with.
I get my "Error!" message on any time on March 3rd. If I use the date of March 4, I get my "Good!" message.
Any ideas?
I also need a check on a specific time on a date two days from now.
For example, the current date is March 1. I need to see if the date value in a specific cell is before or after 8:00 PM on March 3rd.
Here is what I'm starting with.
Code: Select all
Dim stdate As String
stdate = CStr(Date + 2) & "8:00 PM"
If Range("N3") < stdate Then
MsgBox ("Error!")
Exit Sub
Else
MsgBox ("Good!")
End If
Any ideas?
-
- Administrator
- Posts: 80381
- Joined: 16 Jan 2010, 00:14
- Status: Microsoft MVP
- Location: Wageningen, The Netherlands
Re: VBA code for month verification
Instead of using a String variable, use a Date variable:
There is no need to enclose the MsgBox strings in parentheses.
Code: Select all
Dim dtmDate As Date
dtmDate = Date + 2 + TimeSerial(8, 0, 0)
If Range("N3") < dtmDate Then
MsgBox "Error!"
Exit Sub
Else
MsgBox "Good!"
End If
Best wishes,
Hans
Hans
-
- Lounger
- Posts: 35
- Joined: 17 Feb 2010, 22:08
Re: VBA code for month verification
Hi Hans.
Just wanted to let you know this worked. Thanks!
Just wanted to let you know this worked. Thanks!