Macro to make a new tab based on month

TheBlackDragon7

Board Regular
Joined
Jan 23, 2008
Messages
56
I've been trying to build a macro that will create a new tab whenever it's a new month. I have a general idea of how to do it, but I can't get the syntax right for naming the tab the name of the new month. The worksheet that would be copied and placed at the end is named "Template"
 

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.

TheBlackDragon7

Board Regular
Joined
Jan 23, 2008
Messages
56
This is the code I have so far:
Code:
    Sheets("Template").Select
    Sheets("Template").Copy After:=Sheets(2)
    Sheets("Template (2)").Select
    Sheets("Template (2)").Name = "June"
    Range("A4").Select
End Sub

But where it says "June" it needs to name the tab the current month, and the macro needs to run when the month is different from when the book was opened last. Any help is appreciated!
 

jindon

MrExcel MVP
Joined
Aug 21, 2004
Messages
16,995
try
Code:
Sheets("Template").Copy After:=Sheets(2)
ActiveSheet.Name = MonthName(Month(Date),False)
 

Fazza

MrExcel MVP
Joined
May 17, 2006
Messages
9,368
Hi. Maybe like this in the workbook's code module? It checks first if the sheet exists, if not, it adds the new one. HTH, Fazza

Code:
Option Explicit
 
Private Sub Workbook_Open()
 
  If Not SheetExists(Format$(Date, "mmmm yyyy")) Then
    Sheets("Template").Copy After:=Sheets(Sheets.Count)
    ActiveSheet.Name = Format$(Date, "mmmm yyyy")
  End If
End Sub
 
Private Function SheetExists(sname) As Boolean
  'from J Walkenbach
  Dim x As Object
  On Error Resume Next
  Set x = ActiveWorkbook.Sheets(sname)
  If Err = 0 Then SheetExists = True Else SheetExists = False
End Function
 

TheBlackDragon7

Board Regular
Joined
Jan 23, 2008
Messages
56
Thanks jindon!

Here's the code I wrote with that information:

Code:
Private Sub Workbook_Open()
'
' New_month Macro
' Update with new month tab
'
If ActiveSheet.Name = MonthName(Month(Date), False) Then

Else

Sheets("Template").Copy After:=Sheets(2)
ActiveSheet.Name = MonthName(Month(Date), False)
    Range("A4").Select
    
End If
End Sub

It works fine, if you or anyone else has any suggestions on cleaning it up a bit or making it more reliable, I'm all ears!

Thanks again!
 

jindon

MrExcel MVP
Joined
Aug 21, 2004
Messages
16,995
try
Code:
Private Sub Workbook_Open()
Dim ws As Worksheet, wsName As String
wsName = MonthName(Month(Date, False)
On Error Resume Next
Set ws = Me.Sheets(wsName)
On Error GoTo 0
If ws Is Nothing Then
    Me.Sheets("Template").Copy After:=Me.Sheets(2)
    ActiveSheet.Name = wsName
End If
End Sub
 

Watch MrExcel Video

Forum statistics

Threads
1,130,118
Messages
5,640,216
Members
417,131
Latest member
Seanr19871

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
Top