Duplicate, rename sheet but not if it already exist..

buzz71023

Active Member
Joined
May 29, 2011
Messages
295
Office Version
  1. 2016
Platform
  1. Windows
I have this code that duplicated my main sheet and renames is based on a cell value. My problem I don't want to duplicate the sheet if it already exist, which is going to happen A LOT. I need help making this happen. Below is what I have so far.

Code:
Dim MySheetName As String 
    MySheetName = "TestSheet" 
    Sheets("MasterSheet").Copy After:=Sheets("MasterSheet") 
    ActiveSheet.Name = MySheetName
 

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
Try
Code:
Dim MySheetName As String
Dim wsCheck As Worksheet

    MySheetName = "TestSheet"
    On Error Resume Next
    Set wsCheck = Sheets(MySheetName)
    On Error GoTo 0
    If wsCheck Is Nothing Then
      Sheets("MasterSheet").Copy After:=Sheets("MasterSheet")
      ActiveSheet.Name = MySheetName
    End If
 
Last edited:
Upvote 0
What cell has the sheet name assigned to it. How does the cell get it's value.
 
Upvote 0
That worked perfectly! Is there a way to get around the prompt for existing name ranges. I always would click yes.
 
Upvote 0
sorry this is actually what I originally had. I didn't realize I posted one from the research I was doing.
Code:
Dim NameSheet As String

Application.ScreenUpdating = False
    NameSheet = Sheets("Data Sheet").Range("ProductCode").Value
    Sheets("Data Sheet").Copy After:=Sheets("110060-B")
    ActiveSheet.Name = NameSheet
    
Sheets("Data Sheet").Select
Application.ScreenUpdating = True
 
Upvote 0
Is there a way to get around the prompt for existing name ranges. I always would click yes.
I'm not sure of the circumstances here but try
Code:
Application.DisplayAlerts = False
  'Other code here
Application.DisplayAlerts = True
 
Upvote 0
Thanks, works just like I wanted.

To help anyone who may have these same questions later, here was my final code.

Code:
Dim MySheetName As String
Dim wsCheck As Worksheet

Application.ScreenUpdating = False
Application.DisplayAlerts = False
        
    MySheetName = Sheets("Data Sheet").Range("ProductCode").Value
    On Error Resume Next
    Set wsCheck = Sheets(MySheetName)
    On Error GoTo 0
    If wsCheck Is Nothing Then
      Sheets("Data Sheet").Copy After:=Sheets("110060-B")
      ActiveSheet.Name = MySheetName
    End If
    
     Sheets("Data Sheet").Select

Application.ScreenUpdating = True
Application.DisplayAlerts = True
 
Upvote 0

Forum statistics

Threads
1,214,522
Messages
6,120,019
Members
448,938
Latest member
Aaliya13

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