Excel VBA rename sheets from a list

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:

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

  • #1

I have a workbook with multiple tabs/worksheets. I need to rename the tabs based on a list in one of the tab. The list has 2 columns - A and B. There are names of tabs in column B but they needs to be renamed by their corresponding cells in column A. e.g.
column A Column B
abcd rtyu
htyu gui
usa rss
So the macro should search the tab names and if any of the tab is named as in column B (say rss) then it should be renamed to its corresponding entry in column A (i.e. usa in this case). Any guidance will be much appreciated.+

EDIT: The tab name with the list is fixed - "Summary"

Last edited by a moderator: Mar 22, 2018

How to fill five years of quarters?

Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.

Excel VBA rename sheets from a list

Joe4

MrExcel MVP, Junior Admin

  • #2

Welcome to the Board!

Try this:

Code:

Sub MyRenameSheets()

    Dim lrow As Long
    Dim r As Long
    Dim prevNm As String
    Dim newNm As String
    
    Application.ScreenUpdating = False
    
'   Find last row in column A on Summary sheet with data
    lrow = Sheets("Summary").Cells(Rows.Count, "A").End(xlUp).Row
    
    On Error Resume Next
'   Loop through all rows on Summary sheet starting on row 2
    For r = 2 To lrow
'       Capture values
        prevNm = Sheets("Summary").Cells(r, "A")
        newNm = Sheets("Summary").Cells(r, "B")
'       Rename sheets
        Sheets(prevNm).Name = newNm
    Next r
    On Error GoTo 0
    
    Application.ScreenUpdating = True
        
End Sub

  • #3

Thanks a lot. It works perfectly fine and is exactly what was required.

Excel VBA rename sheets from a list

Joe4

MrExcel MVP, Junior Admin

  • #5

Is it possible to have the macro reference a table instead? So for example, if on the "Summary" tab there's a table with multiple columns and column "A" in the example above is in the 1st column of the table and column "B" in the example above is in the 4th column of the table?

Gilbert has a worksheet (named "Control") that contains a list of desired worksheet names in cells A1:A12. He needs a way, in a macro, to rename each of the other 12 worksheets in the workbook based upon that range of cells. The worksheet names don't need to be dynamic; they just need to be renamed when he runs the macro.

The core of developing a macro to address this need is to rely on the Name property of each worksheet you want to rename. For instance, you could use a very simple macro like this:

Sub RenameSheets()
    Dim c As Range
    Dim J As Integer

    J = 0
    For Each c In Range("A1:A12")
        J = J + 1
        If Sheets(J).Name = "Control" Then J = J + 1
        Sheets(J).Name = c.Text
    Next c
End Sub

The macro simply steps through the cell range A1:A12 and, if the next worksheet isn't named "Control," it renames the worksheet to the cell value.

As noted, this macro is very simplistic and should, in all likelihood, be a lot more robust. For instance, what should be done if there are more (or fewer) than 13 worksheets in the current workbook? What should be done if there are empty cells in the range A1:A12? What should be done if someone runs the macro and "Control" isn't the active worksheet? What should be done if there are two identical values in A1:A12? What if there are leading or trailing spaces on one or more names in the range A1:A12? These and (most likely) a whole range of other questions can affect how the macro finally looks. Here's a commented version of the macro that takes into account several of the possibilities just mentioned:

Sub RenameSheets()
    Dim c As Range
    Dim J As Integer
    Dim K As Integer
    Dim sName As String
    Dim w(12) As String
    Dim bGo As Boolean
    Dim sTemp As String

    bGo = True
    If Worksheets.Count <> 13 Then
        ' Check to make sure exactly 13 worksheets in workbook
        bGo = False
        sTemp = "There are more than 13 worksheets."
    End If
    If ActiveSheet.Name <> "Control" Then
        ' Check to make sure Control is active
        bGo = False
        sTemp = "Control worksheet is not active."
    Else
        ' Check for empty and duplicate cells in range
        J = 0
        For Each c In Range("A1:A12")
            sName = Trim(c.Text)
            If sName <> "" Then
                For K = 1 to J
                    If LCase(w(K)) = LCase(sName) Then
                        bGo = False
                        sTemp = "Duplicate sheet names in list."
                    End If
                Next K
                If bGo Then
                    ' Everything still good; add name
                    J = J + 1
                    w(J) = sName
                End If
            End If
        Next c
    End If

    If bGo Then
        K = 0
        For J = 1 To 12
            K = K + 1
            If Sheets(K).Name = "Control" Then K = K + 1
            Sheets(K).Name = w(J)
        Next J
    Else
        MsgBox(sTemp)
    End If
End Sub

Notice how much longer the second version of the macro is than the first? Anytime you start adding multiple checks in a macro, it can really make it much longer than without the checks. The benefit in adding the checks, of course, is that your macro is less likely to run into problems as it is used by people other than you.

ExcelTips is your source for cost-effective Microsoft Excel training. This tip (1506) applies to Microsoft Excel 2007, 2010, 2013, and 2016.

Author Bio

With more than 50 non-fiction books and numerous magazine articles to his credit, Allen Wyatt is an internationally recognized author. He is president of Sharon Parq Associates, a computer and publishing services company. Learn more about Allen...

MORE FROM ALLEN

Creating Custom Labels

There is a whole passel of labels pre-defined in Word. You are not limited to this passel, however; Word allows you to ...

Discover More

End-of-Month Calculations

Don't want to use the EOMONTH function to figure out the end of a given month? Here are some other ideas for discovering ...

Discover More

ExcelTips Ribbon 2019 Archive (Table of Contents)

ExcelTips is a weekly newsletter that provides tips on how to effectively use Microsoft's best-selling ...

Discover More

How do I rename a sheet in a list?

In the Cell group, click on the 'Format' option. Click on the Rename Sheet option. This will get the sheet name into edit mode. Enter the name that you want for the sheet.

How do I rename a sheet in Excel VBA?

Renaming sheets in Excel one can do from the taskbar below the worksheets by double-clicking on them. But in VBA, we use the Sheets or Worksheet property method to rename the sheet. The syntax to rename a sheet is as follows: Sheets(“Old Sheet Name”). Name = “New Sheet name.”

How do I rename multiple sheet names in Excel?

See screenshot:.
Select the worksheets you want to rename from the Worksheets list. Select one type which you want to rename the worksheets under Rename Options. ... .
Select the worksheets you want to rename from the Worksheets list. ... .
Select the worksheets you want to rename from the Worksheets list..