VBA Copies but needs to save name

zendog1960

Active Member
Joined
Sep 27, 2003
Messages
459
Office Version
  1. 2019
Platform
  1. Windows
here is the code I have now...

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    Copy_Sheet
End Sub
Sub Copy_Sheet()
Dim wSht As Worksheet
Dim shtName As String
shtName = Sheets("Location Summary").Range("J11")
For Each wSht In Worksheets
    If wSht.Name = shtName Then
        MsgBox "Sheet already exists...Make necessary " & _
            "corrections and try again."
        Exit Sub
    End If
Next wSht
Sheets("Template").Copy After:=Sheets("Coin Count")
Sheets("Template").Name = shtName
Sheets(shtName).Move After:=Sheets("Location Summary")
Sheets(shtName).Range("A1") = shtName

End Sub

This does what I want except it renames the "Template" sheet to "Template (2)" How can I right the code to preserve the name "Template" so it will continue to work? Once the name changes to Template (2), this code no longer works because it is looking for "Template" and cannot find it.
 

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.
Well for a start you can't have 2 sheets called Template in the same workbook.

So if you aren't deleting or renaming the original then you can't do what you want.
 
Upvote 0
Here's the fix I came up with

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    Copy_Sheet
End Sub
Sub Copy_Sheet()
Dim wSht As Worksheet
Dim shtName As String
shtName = Sheets("Location Summary").Range("J11")
For Each wSht In Worksheets
    If wSht.Name = shtName Then
        MsgBox "Sheet already exists...Make necessary " & _
            "corrections and try again."
        Exit Sub
    End If
Next wSht
Sheets("Template").Copy After:=Sheets("Coin Count")
Sheets("Template").Name = shtName
Sheets(shtName).Move After:=Sheets("Location Summary")
Sheets(shtName).Range("A1") = shtName
Sheets("Template (2)").Name = ("Template")
Sheets("Location Summary").Activate
End Sub

Now one to the next problem I want to tackle. Once the new sheet is created, is there a way to populate the Location Summary on the next available blank line with the cell references in the correct places? Here is the Location Summary Pages....
Coin Shooter.xls
ABCDEF
2ParkTimeTotal CoinsTotal ValueTotal PCVTotal VPH
3Example Park2.504$3.56$0.05$1.42
4
5
6
7
8
9
Location Summary
 
Upvote 0
what I would like is have the new sheet in this case populate the columns A thru F on row 4. When the next new sheet is created, it would populate row 5 with the same information except with the date from the newly created sheet.
 
Upvote 0

Forum statistics

Threads
1,214,965
Messages
6,122,500
Members
449,090
Latest member
RandomExceller01

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