Copy worksheet, buttons and all and rename

Dezertdog

New Member
Joined
Jan 24, 2014
Messages
16
Hello All,
I have searched without success to find code I could adapt to what I want to do.
I want to copy my existing worksheet (named "2014") including the command buttons and the code that goes with them, to a new worksheet (named "2015") and so on every year.
And I would like this to happen automatically on December 28 each year.
I believe this is possible. But I am not versed enough in Excel to accomplish it. (so far)
Here is some code I have been playing with. I know it is far from what I really need.

I have it attached to a command button for quick testing...

Code:
Private Sub CommandButton1_Click()
Dim myWorksheet As Worksheet
    Dim myWorksheetName As String
    myWorksheetName = Format(Now, "yyyy")
    For Each myWorksheet In Worksheets
  
        If myWorksheet.Name = myWorksheetName Then
            MsgBox "Sheet already exists...Make necessary " & _
                "corrections and try again."
            Exit Sub
        End If
       
    Next myWorksheet
        Sheets.Add.Name = myWorksheetName
        Sheets(myWorksheetName).Move Before:=Sheets(Sheets.Count)
       
        
End Sub

Any help would be greatly appreciated.
Thanx in advance

D-Dog
 

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
D-dog

Why don't you change that to "copy" the existing worksheet rather than "add" a worksheet

Assuming the first sheet is the one that you want to clone.

Code:
Private Sub CommandButton1_Click()
Dim myWorksheet As Worksheet
    Dim myWorksheetName As String
    myWorksheetName = Format(Now, "yyyy")
    For Each myWorksheet In Worksheets
  
        If myWorksheet.Name = myWorksheetName Then
            MsgBox "Sheet already exists...Make necessary " & _
                "corrections and try again."
            Exit Sub
        End If
       
    Next myWorksheet

[COLOR=#800080]    Sheets(1).Copy Before:=Sheets(1)
    Sheets(1).Name = myWorksheetName[/COLOR]   [COLOR=#00ff00]' Now a new Sheets(1)[/COLOR]                  End Sub

Note that if you want to automate it, you would work in the Workbook_Open event (in ThisWorkbook code area) to test your date criteria, but if your are going to increment the year why not use something like:

myWorksheetName = Val(Sheets(1).Name) + 1
Sheets(1).Copy Before:=Sheets(1)
Sheets(1).Name = myWorksheetName


Food for thought, but remember if you open the workbook several times during the 28th then it will keep running and adding 2016 etc, so you need another criteria to ensure that it can only be current year + 1
 
Last edited:
Upvote 0
Thank you ozbod, I will play with this a bit and see what i can put together.
I will post if it works.
D-Dog
 
Upvote 0

Forum statistics

Threads
1,214,798
Messages
6,121,635
Members
449,043
Latest member
farhansadik

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