VBA sheet duplicate check

deanw55

New Member
Joined
Aug 30, 2022
Messages
14
Office Version
  1. 365
Platform
  1. Windows
Hi there,
I have a vba script which duplicates a template sheet, then renames it with a name that user types into a cell.
Example:
User inputs the name "Dean" into cell A2 (on sheet "ADD") then runs the macro - a sheet called TEMPLATE is duplicated, then renamed into Dean. I want to check if the name already exists and perform a warning msg box if so. I am using the below code which duplicates the TEMPLATE sheet and instead of giving me a warning it renames it to TEMPLATE (2). Where did I miss the point? Thank you in advance.

1661849367292.png
 

Excel Facts

How to create a cell-sized chart?
Tiny charts, called Sparklines, were added to Excel 2010. Look for Sparklines on the Insert tab.
Hi, normally I use this function routine, which can be tested as a boolean TRUE / FALSE, and called simply by:

VBA Code:
If SheetExists(myNewSheetName) Then

Endif

You would set myNewSheetName variable to the value that the user enters into the cell before checking if it exists.
Rgds
Rob

VBA Code:
Private Function SheetExists(myNewSheetName) As Boolean
'   Returns TRUE if sheet exists in the active workbook
    Dim x As Object
    On Error Resume Next
    Set x = ActiveWorkbook.Sheets(Tabname)
    If Err = 0 Then SheetExists = True _
        Else SheetExists = False
End Function
 
Upvote 0
How about
VBA Code:
Dim myNewSheetName As String
myNewSheetName = Lookup.Range("A4").Value
If myNewSheetName = "" Then
   MsgBox "Sheet name cannot be blank"
   Exit Sub
ElseIf Evaluate("isref('" & myNewSheetName & "'!A1)") Then
   MsgBox myNewSheetName & " is already taken"
   Exit Sub
End If
Template.Copy , Sheets("Template")
ActiveSheet.Name = myNewSheetName
In future please post you code not an image How to Post Your VBA Code
 
Upvote 0
Solution
Thank you Fluff for your answer and suggestion. I'm trying to use your code, but somehow don't know how to place it right into my piece of code to get it working. I assume I also have some unusable lines in my existing code :cautious:

VBA Code:
Sub CreateNewSheet()

    Application.ScreenUpdating = False
    Sheets("TEMPLATE").Visible = True
    Set Template = Sheets("TEMPLATE")
    Set Lookup = Sheets("ADD")
    Dim myNewSheetName
    myNewSheetName = ThisWorkbook.Sheets("ADD").Range("D4")
'   DataSheet = Sheets(myNewSheetName)

On Error Resume Next

If myNewSheetName Is Nothing Then
           
    Template.Select
    ActiveSheet.Copy after:=Sheets("TEMPLATE")
    ActiveSheet.Name = myNewSheetName
    
    Lookup.Select
    Range("D4").Copy
    Sheets(myNewSheetName).Activate
    Range("N2").PasteSpecial xlPasteValues
    Lookup.Select
    Range("D3").Copy
    Sheets(myNewSheetName).Activate
    Range("N3").PasteSpecial xlPasteValues
    Lookup.Select
                    
    Application.CutCopyMode = False
    
    Sheets(myNewSheetName).Activate
    Call LookupIp
        
    Sheets("TEMPLATE").Visible = False
    Application.ScreenUpdating = True
    
Else
    MsgBox ("ALERT! Sheet with this name already existing!")
End If
    
    On Error GoTo 0
  
End Sub
 
Upvote 0
Ok, somehow figured it out and it's working as supposed. Now I have next question.
I am creating new sheets in two steps - first step is to create the sheet and name it with the data entered in D4. Then I copy the values from D3 (ID number) and from D4 (Name of a location), both entered by users, into the new worksheet in cells N2 and N3. But because users could name the location different (in some cases with abbreviations) I could have two sheets for same location with different names - but the ID of the location is exact and cannot change. So I would like to be able to lookup the ID number in the existing sheets (cell N3) and if it exists, tell the user about it and change to that sheet. Else the new sheet with the name D4 should be created. Does it make sense?
 
Upvote 0
As this is a totally different question, you need to start a new thread for it. Thanks
 
Upvote 0
Ok, thank you. I'll start a new thread. I was assuming that I could somehow extend the existing vba code. Thank you.
BR, Dean
 
Upvote 0
Glad to help & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,214,946
Messages
6,122,401
Members
449,081
Latest member
JAMES KECULAH

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