Command button to create a new sheet

Ian1976

Board Regular
Joined
Feb 4, 2016
Messages
139
Office Version
  1. 365
Platform
  1. Windows
Hi All,

Is it possible to have a command button on a sheet which when pressed creates a copy of the sheet and puts it at the end off all the existing sheets but also has the command button on it, so when I've finished filling in the new sheet I can keep creating new sheets?

Thanks
 

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
One way, an active-X command button with this code

Code:
Me.Copy After:= Sheets(Sheets.Count)
 
Upvote 0
Thanks mate, will this also transfer the button onto the new sheet to then create others, sorry not on a pc at the moment! ??
 
Upvote 0
Add a button from the Forms toolbar to the sheet and when you are prompted to Assign Macro select New.

You should now see something like this.
Code:
Option Explicit

Sub Button1_Click()
    
End Sub
Update that code to this to copy the sheet the button is on to the end of the workbook.
Code:
Option Explicit

Sub Button1_Click()
Dim sh As Worksheet

    Set sh = ActiveSheet
    
    sh.Copy after:=Sheets(Sheets.Count)
    
End Sub
Now whenever you click the button the sheet, and the button, will be copied.

The code itself will not be copied but each new button will have this same code assigned to it.
 
Upvote 0
Thanks both options work great, is it possible to be able to name the newly created sheet as "New Log" then "New Log1" ect....

Thanks Guys! Much appreciated!
 
Upvote 0
Would this work if I have forms and buttons on a sheet that i want for the code to transfer over to the new sheet as well?
 
Upvote 0
Would this work if I have forms and buttons on a sheet that i want for the code to transfer over to the new sheet as well?
No, VBA code will not copy over with it.
If you want to know how to do that, that is really a brand new question which warrants its own thread.
 
Upvote 0
is it possible to be able to name the newly created sheet as "New Log" then "New Log1" ect....

This code is for active-x command button

Place VBA in sheet module
Code:
Private Sub CommandButton1_Click()
    Me.Copy After:=Sheets(Sheets.Count)
    Sheets(Sheets.Count).Name = GetName
End Sub

Private Function GetName() As String
    Dim x As Long, n As String
    n = "New Log"
    If SheetExists(n) Then
        Do
            x = x + 1
            If Not SheetExists(n & x) Then Exit Do
        Loop
        n = n & x
    End If
    GetName = n
End Function

Private Function SheetExists(aName As String) As Boolean
    On Error Resume Next
    Dim sh As Worksheet: Set sh = Sheets(aName)
    If Err = 0 Then SheetExists = True Else SheetExists = False
End Function
 
Last edited:
Upvote 0
Form Control button equivalent (see post#9 for active-x)

Place code in standard module
Code:
Sub Button1_Click()
    ActiveSheet.Copy after:=Sheets(Sheets.Count)
    Sheets(Sheets.Count).Name = GetName
End Sub

Private Function GetName() As String
    Dim x As Long, n As String
    n = "New Log"
    If SheetExists(n) Then
        Do
            x = x + 1
            If Not SheetExists(n & x) Then Exit Do
        Loop
        n = n & x
    End If
    GetName = n
End Function

Private Function SheetExists(aName As String) As Boolean
    On Error Resume Next
    Dim sh As Worksheet: Set sh = Sheets(aName)
    If Err = 0 Then SheetExists = True Else SheetExists = False
End Function
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,227
Messages
6,170,849
Members
452,361
Latest member
d3ad3y3

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