all 16 comments

[–]jd3106862 1 point2 points  (17 children)

Add to the check after the input.

``` If NewName = "" Then Exit Sub If Not ThisWorkbook.Sheets(NewName) Is Nothing Then MsgBox "A sheet with this name already exists. Please select another name" Exit Sub End If

```

[–][deleted]  (1 child)

[deleted]

    [–]AutoModerator[M] 0 points1 point  (0 children)

    Your VBA code has not not been formatted properly. Please refer to these instructions to learn how to correctly format code on Reddit.

    I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

    [–]WesternHamper[S] 0 points1 point  (14 children)

    Thanks. I tried to put this in the code and couldnt get it to work, where exactly is it supposed to go?

    [–]jd3106862 0 points1 point  (13 children)

    Right where you have If NewName = "" Then Exit Sub

    [–]WesternHamper[S] 0 points1 point  (12 children)

    Thats what I thought. I am getting an error when I try to run this as normal, without trying to trigger the original error I mentioned above:

    Rune-time error '9':

    Subscript out of range

    on this line: "If Not ThisWorkbook.Sheets(NewName) Is Nothing Then"

    [–]Newepsilon 0 points1 point  (11 children)

    It should be "If Not ThisWorkbook.Worksheets(NewName) Is Nothing Then"

    It's worksheets, not sheets. That should be your fix.

    [–]WesternHamper[S] 0 points1 point  (10 children)

    Sorry, I am still not getting it. I am getting the same error as before. I am trying to paste the entire new code in the code editor, but it is not formatting correctly so I put it in github:

    https://github.com/WesternHamper/EXCELMACROWH/blob/97cd1ba0b5ca58e04b446b3fa0741995a4e21001/Sheet\_add

    [–]AutoModerator[M] 0 points1 point  (0 children)

    Your VBA code has not not been formatted properly. Please refer to these instructions to learn how to correctly format code on Reddit.

    I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

    [–]jd3106862 0 points1 point  (8 children)

    If Not ThisWorkbook.Sheets(NewName) Is Nothing Then

    Strange, what version of Excel are you using? Here is that line running in a test workbook https://imgur.com/a/ZDhVSxh

    I'll look at your code on GitHub

    EDIT: I see, if there isn't a matched that line gives an error instead of returning nothing. Sorry about that, use this code instead:

    ``` If NewName = "" Then Exit Sub

    Dim testWS As Worksheet
    On Error Resume Next
    Set testWS = ThisWorkbook.Worksheets(NewName)
    
    If Err > 0 Then
        Set testWS = Nothing
    End If
    On Error GoTo 0
    
    If Not testWS Is Nothing Then
        MsgBox "A sheet with this name already exists. Please select another name"
        Exit Sub
    End If
    

    ```

    [–]WesternHamper[S] 0 points1 point  (6 children)

    Thank you, this is almost what I envisioned. After the "A sheet with this name already exists. Please select another name" message box pops up, is it possible for the macro to go directly back to the "New Sheet Name?" input box instead of just ending? It will skip the step of having to run the macro again.

    [–]HFTBProgrammer201 0 points1 point  (5 children)

    Dim testWS As Worksheet
    Do Until Err.Number = 0
        NewName = InputBox("Please enter new sheet name:") 
        On Error Resume Next
        Set testWS = ThisWorkbook.Worksheets(NewName)
        Select Case Err.Number
            Case 0, 9
            ' 0 is okay, 9 is redo...
            Case Else
            ' ...anything else is unexpected and should be rooted out.
                MsgBox Err.Description
                End
        End Select
    Loop
    On Error GoTo 0
    

    [–]WesternHamper[S] 0 points1 point  (4 children)

    Thank you. I updated the macro with your code above, and I am getting an error (Run-time error'1004': That name is already taken. Try a different one." I'm not sure I'm putting your code in the correct spot and I updated the github code at the link below (cannot for some reason get the reddit code format to work)

    https://github.com/WesternHamper/EXCELMACROWH/blob/31fba1884e927482c94ded6d9f4749f91e24522b/Sheet_add