Share via

How to reset the sheets number?

Anonymous
2013-03-23T03:10:07+00:00

I would like to reset the sheet number, so after running this macro, whenever I insert a new sheet , the name of sheet always start with sheet1.

Does anyone have any suggestions on how to code it?

Thanks in advance for any suggestions

Microsoft 365 and Office | Excel | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments
Answer accepted by question author
  1. OssieMac 48,001 Reputation points Volunteer Moderator
    2013-03-23T05:52:14+00:00

    If Sheet1 does not exist then when the workbook is closed and re-opened then adding a worksheet it should be Sheet1. This in not always convenient or even possible if running via VBA code within the workbook.

    If it is simply the tab name then you can change the tab name at time of creating the worksheet in VBA like the following. Note that it is necessary to test if the worksheet already exists because if it does then the code will error when attempting to rename.

    Note that if you are creating a temporary worksheet and deleting it from within a VBA code Sub then if created by assigning it to a variable like in the example then you can reference the worksheet by the variable name without ever knowing what the name of the sheet is.  You can even delete it by the variable.

    Sub Macro1()

        Dim wsNew As Worksheet

        Set wsNew = Nothing

        On Error Resume Next

        Set wsNew = Worksheets("Sheet1")

        On Error GoTo 0

        If wsNew Is Nothing Then

            Set wsNew = Sheets.Add(Before:=Sheets(1))

            wsNew.Name = "Sheet1"

        Else

            MsgBox wsNew.Name & " already exists."

            Exit Sub

        End If

    End Sub

    If it is the CodeName that you want to change then see the code in the link below. (The code name is the name that you see in the Project Explorer on the left hand side in the VBA editor. The Code name is the one NOT in parenthesis and the tab name given by the user is the name in parenthesis.)

    It is often preferable to use the code name because it is somewhat more difficult for the end user to change it.

    If you use the code example at the following link then you will need to select Tools menu  and then References and scroll down to Microsoft Visual basic for applications extensibility 5.3 and check the box. (Ensure you check the box; not highlight the line) and then click OK.

    http://www.tek-tips.com/faqs.cfm?fid=4090

    0 comments No comments

4 additional answers

Sort by: Most helpful
  1. Anonymous
    2013-03-25T02:38:48+00:00

    I would like to reset the sheet number, so after running this macro, whenever I insert a new sheet , the name of sheet always start with sheet1.

     

    Does anyone have any suggestions on how to code it?

     

    Thanks in advance for any suggestions

    Not sure if this idea would help.

    Place this code in the "ThisWorkbook" module.

    When you are ready to insert a new worksheet, just click the "+" at the bottom row of tabs.

    This will insert a new workseet at the beginning, and rename all the sheets at the same time.

    Private Sub Workbook_NewSheet(ByVal Sh As Object)

        Dim J As Long

        '// Move new sheet to the beginning

           Sh.Move Before:=Sheets(1)

        '// Rename all sheets beginning with 1

        '// 'xx is used to avoid duplicate names at this stage

        For J = Sheets.Count To 1 Step -1

            Sheets(J).Name = "xxSheet" & FormatNumber(J, 0)

        Next J

        '// Clean up Names

        For J = 1 To Sheets.Count

            Sheets(J).Name = Mid$(Sheets(J).Name, 3)

        Next J

    End Sub

    = = = = = = = = = =

    HTH  :>)

    ~Dana D

    1 person found this answer helpful.
    0 comments No comments
  2. Anonymous
    2013-03-23T12:12:44+00:00

    Thanks everyone very much for suggestions

    0 comments No comments
  3. Anonymous
    2013-03-23T12:01:19+00:00

    I would like to reset the sheet number, so after running this macro, whenever I insert a new sheet , the name of sheet always start with sheet1.

     

    Does anyone have any suggestions on how to code it?

     

    Thanks in advance for any suggestions

    Hi,

    Do you mean you want to sort the worksheets? If so try this code.

    Sub SortSheets()

    Dim x As Long, y As Long

    For x = 1 To Worksheets.Count

        For y = x To Worksheets.Count

            If UCase(Sheets(y).Name) < UCase(Sheets(x).Name) Then

                  Sheets(y).Move before:=Sheets(x)

             End If

        Next

    Next

    End Sub

    0 comments No comments
  4. Deleted

    This answer has been deleted due to a violation of our Code of Conduct. The answer was manually reported or identified through automated detection before action was taken. Please refer to our Code of Conduct for more information.


    Comments have been turned off. Learn more