Slightly modify the code [VBA]

Martin_H

Board Regular
Joined
Aug 26, 2020
Messages
190
Office Version
  1. 365
Platform
  1. Windows
Hi team,

I would like to slightly modify the code below, which is a small part of bigger macro:

VBA Code:
If Month(Date) < 3 Then
MsgBox "Not yet available.", vbExclamation
Exit Sub
End If

So basically, if today's month is February and I am trying to run macro specified only for the month of March, code will tell me "Not yet available."
If today's month is March and I am trying to run macro specified for the month of March, code will continue without any message box.

As you can see, code compare today's date to the specific month (3rd month in this case.
I need to inclued today's year to the code.

For example:
Today's year is 2022.
Today's month is February (2).
So when I run macro specified for March, it will obviously stop, because it is only February, but when I open Workbook from year 2021 and run macro specified for the month of March (2021) it will continue because that is the past.

I hope it is clear, if not let me know please and I will be more specific.
 

Excel Facts

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result
Just like you can pull the month from a date like you are:
VBA Code:
Month(Date)
you can also pull the year from a date in a similar fashion, i.e.
VBA Code:
Year(Date)

So you should be able to incorporate a year comparison in your code, just like you have a month one.
You will probably need both, in combination or in a nested situation.
I would probably do the YEAR check first, then the MONTH one.
 
Upvote 0

Forum statistics

Threads
1,224,739
Messages
6,180,676
Members
452,993
Latest member
FDARYABEE

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