Search code examples
excelexcel-2010excel-2007

Rename sheet name to table name


I imported a bunch of data into my Excel from SQL. So I have a bunch of sheets like so:

enter image description here

Each sheet has a table name that I can view in the table view area here:

enter image description here

I was wondering if there is any way to mass re-name the sheets to what the table name is.


Solution

  • Try something like this

    Sub RenameSheetToTableName()
        Dim ws As Worksheet
        Dim tbl As ListObject
    
        For Each ws In ThisWorkbook.Sheets
            On Error Resume Next
            Set tbl = ws.ListObjects(1)
            On Error GoTo 0
            If Not tbl Is Nothing Then ws.Name = tbl.Name
        Next ws
    End Sub