Hello! This is my first post to this forum. As the saying goes, I know enough to be dangerous, but now I need some help beyond my current knowledge.
I have a workbook that contains numerous worksheets, one each for different cities, and each worksheet name is based on the value in a named cell, "City_Name".
I am using the VBA code (below) to automatically name the worksheet tab the same value as the referenced cell, "City_Name". The code is located in each sheet as a Microsoft Excel Object.
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
'
ActiveSheet.Name = Range("City_Name").Value
End Sub
However, here's is one problem I can't find a solution to. When I copy an existing sheet to create a duplicate, I get a "Run-time error '1004': That name is already taken. Try a different one."
I know the error is happening because the duplicate sheet, is of course, duplicating everything form the original sheet, including the value in the named cell, "City_Name". So the minute the duplicate sheet is created, the VBA code runs to rename the sheet, but it can't because it would be a duplicate name.
So, (finally!), here's my question - Is there a way duplicate a sheet and pause the VBA code until the sheet is renamed?, then have the VBA code run as normal, and still as an Excel Object?
I know there are methods for running a macro to copy a sheet and rename it, but I haven't found a way to keep a constant link to the the cell value, the way the above code works. This constant linkage makes this code so handy.
Thanks in advance for any suggestions!
I have a workbook that contains numerous worksheets, one each for different cities, and each worksheet name is based on the value in a named cell, "City_Name".
I am using the VBA code (below) to automatically name the worksheet tab the same value as the referenced cell, "City_Name". The code is located in each sheet as a Microsoft Excel Object.
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
'
ActiveSheet.Name = Range("City_Name").Value
End Sub
However, here's is one problem I can't find a solution to. When I copy an existing sheet to create a duplicate, I get a "Run-time error '1004': That name is already taken. Try a different one."
I know the error is happening because the duplicate sheet, is of course, duplicating everything form the original sheet, including the value in the named cell, "City_Name". So the minute the duplicate sheet is created, the VBA code runs to rename the sheet, but it can't because it would be a duplicate name.
So, (finally!), here's my question - Is there a way duplicate a sheet and pause the VBA code until the sheet is renamed?, then have the VBA code run as normal, and still as an Excel Object?
I know there are methods for running a macro to copy a sheet and rename it, but I haven't found a way to keep a constant link to the the cell value, the way the above code works. This constant linkage makes this code so handy.
Thanks in advance for any suggestions!