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

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
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!
 
Upvote 0
try
Code:
Sheets("Template").Copy After:=Sheets(2)
ActiveSheet.Name = MonthName(Month(Date),False)
 
Upvote 0
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
 
Upvote 0
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!
 
Upvote 0
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
 
Upvote 0

Forum statistics

Threads
1,214,626
Messages
6,120,602
Members
448,974
Latest member
ChristineC

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