SOLVED

I would like to rename all the sheets from a list of names I have

Copper Contributor

I have 112 sheets and 112 names, I would like to rename all of the sheets. How can I do this?

10 Replies
best response confirmed by Hursh Patel (Copper Contributor)
Solution

Hi Hursh,

 

You can do this by using a VBA code!

But you need to have these name listed in a worksheet stating from cell A1 as follows:

List of names.png

 

Then you can depend on this code to rename all worksheets at once:

Sub rename()

    Dim r As Integer
    r = 1
    
    For Each Sheet In Sheets
        Sheet.Name = Cells(r, 1).Value
        r = r + 1
    Next

End Sub

 

This code will loop through each worksheet and rename the Sheet1 with the name of cell A1, Sheet2 with the name of cell A2, and so on.

 

Please check this link to learn how to insert and run this code.

 

Regards

How would you do the reverse? Capture the list of all the worksheet names.

You would change

Sheet.Name = Cells(r, 1).Value

to

Cells(r, 1).Value = Sheet.Name

Please note:  This code  will overwrite any data that is in the cells in column A of the worksheet that is active when you run it.

Can this macro be done with the Name in $B$2? @Haytham Amairah 

@teri_chow

 

Hi,

 

If you mean that the list of names starts from cell B2, you can change the code as follows:

Sub rename()
    'Set the starting cell
    Dim cell As Range
    Set cell = Range("B2")
    
    Dim r As Long
    r = 0
    
    For Each Sheet In Sheets
        Sheet.Name = cell.Offset(r, 0)
        r = r + 1
    Next
End Sub

 

Regards

@Haytham Amairah

 

How do you begin renaming sheets at Sheet 3? The first sheet is a summary sheet with a list project numbers I want to rename each sheet to and the second sheet is for rates. The list begins in Cell A2 and progresses downward.  In addition, there will be two sheets per project: one for man hours and one for cost. For example, if cell A2 on the summary sheet contains the project number 12345, then Sheet 3's name would be 12345 Hrs and Sheet 4's name would be 12345 Cost. If cell A3 on the Summary sheet had 67890, the Sheet 5 would show 67890 Hrs and Sheet 6 would show 67890 Cost.

When I use this exact code with the list of values shown, I get Run-time error '1004': Application-defined or object-defined error.

@mjohnston02090819 

Could you attach a small sample workbook demonstrating the problem (without sensitive data), or if that is not possible, make it available through OneDrive, Google Drive, Dropbox or similar?

@mjohnston02090819 

 

I wondered the same thing.  I eventually figured out you just change the J value and assign it to the sheet as (J) so it picked the right one.  It's about 6 months too late for your question, but hopefully it'll help others that look here.

 

Sub Rename()
    Dim J As Integer
    Dim r As Integer
    r = 1
    J = 2
    For Each Sheet In Sheets
        J = J + 1
        Sheets(J).Name = "Sheet" & J
        On Error Resume Next
    Next
    J = 2
    For Each Sheet In Sheets
        J = J + 1
        Sheets(J).Name = Cells(r, 1).Value
        On Error Resume Next
        r = r + 1
    Next
End Sub

1 best response

Accepted Solutions
best response confirmed by Hursh Patel (Copper Contributor)
Solution

Hi Hursh,

 

You can do this by using a VBA code!

But you need to have these name listed in a worksheet stating from cell A1 as follows:

List of names.png

 

Then you can depend on this code to rename all worksheets at once:

Sub rename()

    Dim r As Integer
    r = 1
    
    For Each Sheet In Sheets
        Sheet.Name = Cells(r, 1).Value
        r = r + 1
    Next

End Sub

 

This code will loop through each worksheet and rename the Sheet1 with the name of cell A1, Sheet2 with the name of cell A2, and so on.

 

Please check this link to learn how to insert and run this code.

 

Regards

View solution in original post