Modifying code so that Month feature updates automatically

Enzo_Matrix

Board Regular
Joined
Jan 9, 2018
Messages
113
Is there a way to modify this so that in March, it will transition automatically to April, April -> May and so forth?

Code:
Sub NewMonth()'
' NewMonth Macro
'
' Keyboard Shortcut: Ctrl+Shift+S
'
    Sheets(2).Select
    Sheets(2).Copy Before:=Sheets(3)
    Sheets(3).Select
    Sheets(3).Name = "March - On-Time"
    ActiveCell.FormulaR1C1 = "March - Mississauga"
    Range("A4").Select
    ActiveCell.FormulaR1C1 = "3/1/2018"
    Range("A5").Select
    ActiveCell.FormulaR1C1 = "3/2/2018"
    Range("A4:A5").Select
    Selection.AutoFill Destination:=Range("A4:A23"), Type:=xlFillDefault
    Range("A4:A23").Select
    Range("I2").Select
    ActiveSheet.PivotTables("PivotTable3").ChangePivotCache ActiveWorkbook. _
        PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
        "C:\Users\jbotha\Desktop\on-time\[Blank template.xlsm]March - On-Time!R3C1:R23C5" _
        , Version:=xlPivotTableVersion15)
    Sheets("Month By Month Comparison").Select
    Range("C5").Select
    ActiveCell.FormulaR1C1 = "='March - On-Time'!R[20]C[2]"
    Range("C6").Select
    Sheets("Feb - On-Time").Select
    ActiveWindow.SelectedSheets.Visible = False
End Sub
 

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
This will provide the name of the month following the current month
Code:
Dim Mnth as string
Mnth = MonthName(Month(Application.EOMonth(Date, 0) + 1))
 
Upvote 0
@JoeMo

I am still new to VBA and used the Macro recorder to generate the code listed above. Where would I input your code to make it fit within mine?
 
Upvote 0
Use the Mnth variable in place of every string where you have "March". For example, this:

Sheets(3).Name = "March - On-Time"

becomes:

Sheets(3).Name = Mnth & " - On-Time"
 
Upvote 0

Forum statistics

Threads
1,224,819
Messages
6,181,153
Members
453,021
Latest member
Justyna P

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