Creating new sheet with user input

deanw55

New Member
Joined
Aug 30, 2022
Messages
14
Office Version
  1. 365
Platform
  1. Windows
Hi,
I have a workbook with lots of existing sheets which is manipulated by many users. The first sheet is called Lookup and on this sheet the user enters data in 2 cells (D3 = ID of the location and D4 = name of the location). The script then checks if a sheet with that same name (D4) exists and creates a new sheet with that name or rejectes the creation. But I'm getting into trouble because users may use different names for the same location (abbreviations and so on) so I want to be sure, that no sheet with that name exists AND no other sheet includes that location ID (which is unique for every location). This location ID stands in every existing sheet in the cell N3. Could you please help me out? Thank you in advance. I would like to:
1. Unhide the sheet named Template (that sheet serves as a template for new sheets)
1. Check if the users entered something in D4
2. Warn the user if the cell is empty (msgbox) else proceed
3. Check the workbook for existing sheets with the name given in D4
4. If a sheet with that name exists then warn the user (msgbox) and exit sub else proceed
5. If a sheet with this name doesn't exist check the location ID given in D3 in all existing sheets in cell N3
6. Warn the user if a sheet already contains that location ID (msgbox) and exit sub or create the new sheet with that name given in D4
7. Copy values from the Lookup sheet cells D3 and D4 and copy them into the newly created sheet into cells N3 and N4

Below is the code I'm using right now and it's working smooth - just missing that part with the location ID check. Thank you all for your help.

VBA Code:
Sub CreateNewSheet()

    Application.ScreenUpdating = False
    Sheets("TEMPLATE").Visible = True
    Set Template = Sheets("TEMPLATE")
    Set Lookup = Sheets("Lookup")
    Dim myNewSheetName As String
    Dim myNewSheetID As String
    myNewSheetName = Lookup.Range("D4").Value
    myNewSheetID = Lookup.Range("D3").Value

If myNewSheetName = "" Then
   MsgBox "Sheet name cannot be blank"
   Sheets("TEMPLATE").Visible = False
   Exit Sub
ElseIf Evaluate("isref('" & myNewSheetName & "'!D4)") Then
   MsgBox myNewSheetName & " is already taken"
   Sheets(myNewSheetName).Activate
   Sheets("TEMPLATE").Visible = False
   Exit Sub
End If
Template.Copy , Sheets("Template")
Sheets("TEMPLATE").Visible = False
ActiveSheet.Name = myNewSheetName

Lookup.Select
Range("D3:D4").Copy
Sheets(myNewSheetName).Activate
Range("N2").PasteSpecial xlPasteValues
Call LookupIp
Application.ScreenUpdating = True

End Sub
 

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
Hi,
see if this update to your code does what you want

VBA Code:
Sub CreateNewSheet()
    Dim wsTemplate      As Worksheet, ws As Worksheet
    Dim myNewSheetName  As String
    Dim myNewSheetID    As String
   
    With ThisWorkbook
        Set Lookup = .Worksheets("Lookup")
        Set wsTemplate = .Worksheets("TEMPLATE")
    End With
   
    myNewSheetName = Lookup.Range("D4").Value
    myNewSheetID = Lookup.Range("D3").Value
   
    If myNewSheetName = "" Then
        'check blank entry
        MsgBox "Sheet name cannot be blank", 16, "Sheet Name Required"
       
    ElseIf Evaluate("isref('" & myNewSheetName & "'!D4)") Then
        'check existing sheet name
        MsgBox myNewSheetName & " Is already taken", 48, "Sheet Exists"
        Worksheets(myNewSheetName).Activate
       
    Else
        'check existing ID
        For Each ws In ThisWorkbook.Worksheets
            If ws.Range("N3").Value = myNewSheetID Then
                MsgBox "Location ID " & myNewSheetID & Chr(10) & _
                "Already Exists In Sheet " & ws.Name, 16, "ID Exists"
                Exit Sub
            End If
        Next ws
       
    Application.ScreenUpdating = False
   
        With wsTemplate
            .Visible = True
            .Copy , wsTemplate
            .Visible = False
        End With
       
        ActiveSheet.Name = myNewSheetName
       
        Lookup.Range("D3:D4").Copy
        Worksheets(myNewSheetName).Range("N2").PasteSpecial xlPasteValues
        Call LookupIp
       
    End If
   
    With Application
        .CutCopyMode = False: .ScreenUpdating = True
    End With
   
End Sub

Dave
 
Upvote 0
Solution
most welcome & appreciate your feedback

Dave
 
Upvote 0
Hi,
I did some tests now. It's sadly not working as supposed. When I enter a already taken ID in the cell D3 and a new location name in D4 it creates a new sheet with the new name and the ID which exists in another sheet. Is it maybe the part below 'check existing ID at the if statement (ws.Range only?). Should there maybe be defined which worksheet (ws)?

VBA Code:
            If ws.Range("N3").Value = myNewSheetID Then

VBA Code:
Sub CreateNewSheet()
    Dim wsTemplate      As Worksheet, ws As Worksheet
    Dim myNewSheetName  As String
    Dim myNewSheetID    As String
   
    With ThisWorkbook
        Set Lookup = .Worksheets("Lookup")
        Set wsTemplate = .Worksheets("TEMPLATE")
    End With
   
    myNewSheetName = Lookup.Range("D4").Value
    myNewSheetID = Lookup.Range("D3").Value
   
    If myNewSheetName = "" Then
        'check blank entry
        MsgBox "Sheet name cannot be blank", 16, "Sheet Name Required"
       
    ElseIf Evaluate("isref('" & myNewSheetName & "'!D4)") Then
        'check existing sheet name
        MsgBox myNewSheetName & " Is already taken", 48, "Sheet Exists"
        Worksheets(myNewSheetName).Activate
       
    Else
        'check existing ID
        For Each ws In ThisWorkbook.Worksheets
            If ws.Range("N3").Value = myNewSheetID Then
                MsgBox "Location ID " & myNewSheetID & Chr(10) & _
                "Already Exists In Sheet " & ws.Name, 16, "ID Exists"
                Exit Sub
            End If
        Next ws
       
    Application.ScreenUpdating = False
   
        With wsTemplate
            .Visible = True
            .Copy , wsTemplate
            .Visible = False
        End With
       
        ActiveSheet.Name = myNewSheetName
       
        Lookup.Range("D3:D4").Copy
        Worksheets(myNewSheetName).Range("N2").PasteSpecial xlPasteValues
        Call LookupIp
       
    End If
   
    With Application
        .CutCopyMode = False: .ScreenUpdating = True
    End With
   
End Sub
 
Upvote 0
Is the ID numeric, Alpha or Alpha Numeric?

Dave
 
Upvote 0
with that scenario it seems to work ok for me - have you made any changes to the code?

1662017224458.png
 
Upvote 0
no worries glad resolved & appreciate feedback

Dave
 
Upvote 0

Forum statistics

Threads
1,215,695
Messages
6,126,263
Members
449,307
Latest member
Andile

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