VBA: Input current YYYY-MM(+1) into a specific cell

larinda4

Board Regular
Joined
Nov 15, 2021
Messages
73
Office Version
  1. 365
Platform
  1. Windows
Good morning,

I'm having a hard time trying to figure out a macro that will update the yyyy-mm(+1) in a specific cell. It needs to include the ' at the beginning as that year-month is used to update the table below it.

Example: It is currently September 2022 right now. I would need the macro to select cell C2, and update it to '2022-10

I found this code below that I was thinking maybe I could alter?

VBA Code:
Sub TestDate()
   Dim dtToday As String
   dtToday = Format (Date, "yyyy mm")
End Sub

Any help would be appreciated!
 
It really depends on what it is you are trying to do.
If this follows you original question, and you ALWAYS want the following month, then use the logic from the line of code I gave you originally, i.e.
Rich (BB code):
ActiveSheet.Name = Format(DateAdd("m", 1, Date), "yyyy mm") & "Final - By Region"
Thanks for taking the time to explain that to me. You've definitely saved me from potential errors in the future.

I wish I could buy you a coffee!
 
Upvote 0

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
You are welcome.
Glad I was able to help!

Yes, what you had originally would have worked for the rest of the month of September, but then when you hit October, it would returned "2022 Ocotber..." until October 16, at which point it would have switched over to "November 2022".
 
Upvote 0

Forum statistics

Threads
1,215,165
Messages
6,123,390
Members
449,098
Latest member
ArturS75

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