Duplicate and rename based on cell range

Oldat43

New Member
Joined
Nov 16, 2021
Messages
12
Office Version
  1. 365
Platform
  1. Windows
Hi all.

I have been tasked with updating the excel files used in my job, a lot of the staff are very very basic with anything computer related so I am trying to make the files more user friendly.
I work in a charity that depends on funding so asyou can imagin its hard to get skilled staff. I have an ok understanding of excel and how macros work but I am very new to VBA and was wonder would anyone help me figure my way through a couple of issues.

I have 2 wrok sheets one called Main and one called Template.
I need to create a VBA button on Main that will duplicate Template and name it from the value in cell A1 on the Main worksheet.

Heres the code I have so far.
Sub testcopy()
Dim wks As Worksheet
Set wks = ActiveSheet
ActiveSheet.Copy after:=Worksheets(Sheets.Count)
ActiveSheet.Name = wks.Range("A1").Value
wks.Activate
End Sub

When I run it, it dups the page that is active and names it with the value in cell A1 of the active sheet.
How do I make it copy the Template sheet and rename from A1 on the Main sheet?

Thenks very much for the help.
 

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
Hello. Try:

VBA Code:
Dim wsTem As Worksheet, wsMain as Worksheet
Set wsTem = Sheets("Template")
Set wsMain = Sheets("Main")
wsTem.Copy after:=Worksheets(Sheets.Count)
ActiveSheet.Name = wsMain.Range("A1").Value
wsMain.Activate
 
Upvote 0
Hello. Try:

VBA Code:
Dim wsTem As Worksheet, wsMain as Worksheet
Set wsTem = Sheets("Template")
Set wsMain = Sheets("Main")
wsTem.Copy after:=Worksheets(Sheets.Count)
ActiveSheet.Name = wsMain.Range("A1").Value
wsMain.Activate
That works perfect...

Heres another problem, is there a way to stop it working or trow up an error if the sheet name already exsists?
 
Upvote 0
I like to use a function:

VBA Code:
Function SheetExists(shtName As String) As Boolean

Dim sht As Worksheet

On Error Resume Next
Set sht = Sheets(shtName)
On Error GoTo 0
SheetExists = Not sht Is Nothing

End Function

Sub testcopy()

Dim wks As Worksheet
Set wsTem = Sheets("Template")
Set wsMain = Sheets("Main")

If Not SheetExists(wsMain.Range("A1").Value) Then
    wsTem.Copy after:=Worksheets(Sheets.Count)
    ActiveSheet.Name = wsMain.Range("A1").Value
    wsMain.Activate
End If

End Sub
 
Upvote 1
Solution
I like to use a function:

VBA Code:
Function SheetExists(shtName As String) As Boolean

Dim sht As Worksheet

On Error Resume Next
Set sht = Sheets(shtName)
On Error GoTo 0
SheetExists = Not sht Is Nothing

End Function

Sub testcopy()

Dim wks As Worksheet
Set wsTem = Sheets("Template")
Set wsMain = Sheets("Main")

If Not SheetExists(wsMain.Range("A1").Value) Then
    wsTem.Copy after:=Worksheets(Sheets.Count)
    ActiveSheet.Name = wsMain.Range("A1").Value
    wsMain.Activate
End If

End Sub
Thats fantastic. Thank you very much.

Can I pick your brain one more time.
Can you add that it switches to the sheet that was just created?
At the moment when I click the create button it stays on the Main sheet.

Thank you so much for the help.
 
Upvote 0

Forum statistics

Threads
1,214,983
Messages
6,122,592
Members
449,089
Latest member
Motoracer88

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