Setting worksheet name not working?

jkwleisemann

New Member
Joined
May 31, 2016
Messages
19
I'm trying to set the name of a worksheet using the following code, and it's failing to process for some reason. Can anybody help me out with this?

Code:
Sub CreateNew()
    intTabs = Worksheets(1).Cells(18, 2).Value
    For i = 1 To intTabs
        strWsName = InputBox("Please name Sheet" & i & ".", "Sheet" & i & " Name", "Sheet" & i)
        If i > 1 Then Worksheets(5).Copy After:=Worksheets(Worksheets.Count)
        Worksheets(Worksheets.Count).Name = strWsName
    Next
End Sub

Also, if there's any way for me to try and set the name when I'm doing the Copy method, that'd be great. I'd like to try and minimize the number of chances to throw "errors" that aren't.
 

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.
Try this more robust code. It handles the possibility of the user entering a sheet name which already exists, looping until a unique name is entered, and clicking Cancel on the InputBox prompt.

Code:
Sub CreateNew()
    Dim intTabs As Integer, i As Integer, strWsName As String, sheetNameExists As Boolean, cancelled As Boolean
    intTabs = Worksheets(1).Cells(18, 2).Value
    i = 0
    Do
        i = i + 1
        Do
            strWsName = InputBox("Please name Sheet " & i & ".", "Sheet " & i & " Name", "Sheet" & i)
            If strWsName = "" Then
                cancelled = True
            Else
                sheetNameExists = SheetExists(strWsName)
                If sheetNameExists Then MsgBox "The sheet named '" & strWsName & "' already exists"
            End If
        Loop Until cancelled Or Not sheetNameExists
        If Not cancelled Then
            Worksheets(5).Copy After:=Worksheets(Worksheets.Count)
            Worksheets(Worksheets.Count).Name = strWsName
        End If
    Loop While i < intTabs And Not cancelled
End Sub

Private Function SheetExists(sheetName As String) As Boolean
    Dim ws As Worksheet
    On Error Resume Next
    Set ws = Worksheets(sheetName)
    On Error GoTo 0
    SheetExists = Not ws Is Nothing
End Function
You can't copy and name a sheet in one step.
 
Upvote 0
Had a feeling I couldn't do the copy and name in one step.

And actually, I just discovered that what created the error was that I had the workbook protected. So nice of Excel to elaborate on these sort of things!
 
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,284
Members
452,630
Latest member
OdubiYouth

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