Getting things to happen in particular months

Red over White

Board Regular
Joined
Jul 16, 2011
Messages
129
Office Version
  1. 365
Platform
  1. MacOS
I am trying to write a VBA program where actions are dependent on what the current month is. In the example below, in February, May, August or November one set of actions will happen and in the remaining months another set of actions will occur.
If the macro was run today, I would be looking for the program to be picking up on the “=11” element.
What I’m struggling what is what the correct TODAY/MONTH combination for this to happen, bearing in mind I use UK date formats. I have tried various combinations of TODAY, MONTH and DATE, with and without brackets, but have not had any success

If TODAY/MONTH combination = 2 Or 5 Or 8 Or 11 Then
One set of actions
End if
If TODAY/MONTH combination <> 2 Or 5 Or 8 Or 11 Then
Another set of actions
End if

If someone does come up with a solution, I would be grateful if they could clarify if I need to put leading zeroes in front of the single digit months in readiness for next year.

Thanks
 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
If you want the month of today you can use
If Month(Now) = 11 Or 8 Or 5 Or 2 Then
set of action
Else
Set of another action
End if

Your date format should not be relevant as Month formula will return a number from 1 to 12.
And with else it wil ltrigger for all case that are not already accounted for. No need to do a 2nd if this way.
 
Upvote 0
Thanks for the response, but having copied that line into my macro I'm getting "Compile error - Wrong number of arguments or invalid property assignment" with Month highlighted
 
Upvote 0
Hi,

does this help you ?

VBA Code:
Sub test()

this_month = Month(Now())

Select Case this_month

    Case 2, 5, 8, 11
        MsgBox ("2,5,8,11")
    Case 1, 3
        MsgBox ("1,3")
    Case 4, 9
        MsgBox ("4,9")
    Case Else
        MsgBox ("anything else goes here...")
    
End Select

End Sub
 
Upvote 0
When in VBA if you type the formula manually when you open the parenthese it show you how many argument. For me it should be only one; the date. Now being the date and hour of when the vba is running
1698944731538.png


If this does not work for you try adding an opening and closing parenthese after Now.
 
Upvote 0
Hi,

does this help you ?

VBA Code:
Sub test()

this_month = Month(Now())

Select Case this_month

    Case 2, 5, 8, 11
        MsgBox ("2,5,8,11")
    Case 1, 3
        MsgBox ("1,3")
    Case 4, 9
        MsgBox ("4,9")
    Case Else
        MsgBox ("anything else goes here...")
   
End Select

End Sub
I'm getting the same error message as before with Month again highlighted
 
Upvote 0
As @Proulxs mentioned.. do you have the correct number of brackets ? else a reboot, as not sure why it would stop for you ?
 
Upvote 0
As @Proulxs mentioned.. do you have the correct number of brackets ? else a reboot, as not sure why it would stop for you ?
I attach a copy of the two macros, and the error message that I get on the second macro. It doesn't appear to like "month", I have tried capitalising the "M", but it reverts to lower case.
 

Attachments

  • Screenshot 2023-11-02 at 17.19.08.png
    Screenshot 2023-11-02 at 17.19.08.png
    84.5 KB · Views: 17
Upvote 0
So the error is in your own macro, rather than in the code I sent you ?

Can you run my code and see what you get please ?
 
Upvote 0
I get the same error message on both macros.
 

Attachments

  • Screenshot 2023-11-02 at 17.26.07.png
    Screenshot 2023-11-02 at 17.26.07.png
    63.7 KB · Views: 15
Upvote 0

Forum statistics

Threads
1,223,886
Messages
6,175,198
Members
452,617
Latest member
Narendra Babu D

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