Macro for Excel tabs

amattey

New Member
Joined
Jul 18, 2011
Messages
46
Hello,

Can anyone help me with a code that will copy a complete sheet including the macros within it and create another tab within the same workbook and name the sheet with the current month and year? Kinda like when I run that macro it will create a tab with the tab name for example "August, 2011" and will be an exact copy of the original sheet including its macros.
 

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.
Something like this?
Code:
Sub NewMonthMacro()
 
  ActiveSheet.Copy After:=Sheets(Sheets.Count)
  ActiveSheet.Name = Format(Now(), "mmmm, yyyy")
 
End Sub
There's no error checking in that code so it will fail if the sheet already exists
 
Last edited:
Upvote 0
With error checking:-
Code:
Sub NewMonthMacro()
 
  ActiveSheet.Copy After:=Sheets(Sheets.Count)
  On Error Resume Next
  ActiveSheet.Name = Format(Now(), "mmmm, yyyy")
  If Err.Number = 0 Then
    MsgBox "Sheet '" & Format(Now(), "mmmm, yyyy") & "' created." _
         & Space(10), vbOKOnly + vbInformation
  End If
  If Err.Number = 1004 Then
    Application.DisplayAlerts = False
    ActiveSheet.Delete
    Application.DisplayAlerts = True
    MsgBox "Sheet '" & Format(Now(), "mmmm, yyyy") & "' already exists!" _
         & Space(10), vbOKOnly + vbExclamation
  End If
  On Error GoTo 0
 
End Sub
 
Upvote 0
Is there a macro I can also run to take me from the current tab to the tab that was created with the current month and year as you helped me above?
 
Upvote 0
I didn't expect you to ask me that... doesn't the new sheet get activated by default?
 
Upvote 0
Yes it does but I have a fixed sheet that I want to make sure that the fixed sheet can be linked directly to the new one created.
 
Upvote 0
Ah! Something like this then:-
Code:
Sub JumpToThisMonth()
 
  On Error Resume Next
  Sheets(Format(Now(), "mmmm, yyyy")).Activate
  If Err.Number = 9 Then
    MsgBox "Sheet '" & Format(Now(), "mmmm, yyyy") & "' doesn't exist!" _
         & Space(10), vbOKOnly + vbExclamation
  End If
  On Error GoTo 0
 
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,521
Messages
6,179,278
Members
452,902
Latest member
Knuddeluff

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