• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

VBA Code Revision - define range as table in multiple tabs/worksheets

AK12

New Member
Hi,

The following code works to define ranges in 200 plus tabs in a workbook but it names each table Table1, 2, 3, and so on.

can any one revise the code to provide the worksheet name as table name instead please?

Thank you
Code:
Sub AddTablesIfNone()
Dim ws As Excel.Worksheet
For Each ws In ActiveWorkbook.Worksheets
        With ws
            If .UsedRange.ListObject Is Nothing Then
                .ListObjects.Add SourceType:=xlSrcRange, Source:=.UsedRange, xllistobjecthasHeaders:=xlYes
            End If
        End With
    Next ws
    End Sub
>>> use code - tags <<<
 

Attachments

  • 1583444206719.png
    1583444206719.png
    19.8 KB · Views: 2
Last edited by a moderator:
untested:
Code:
Sub AddTablesIfNone()
Dim ws As Excel.Worksheet
For Each ws In ActiveWorkbook.Worksheets
  With ws
    If .UsedRange.ListObject Is Nothing Then
      With .ListObjects.Add(SourceType:=xlSrcRange, Source:=.UsedRange, xllistobjecthasHeaders:=xlYes)
        On Error Resume Next 'in case there's already a table with the same name.
        .Name = ws.Name
        On Error GoTo 0
      End With
    End If
  End With
Next ws
End Sub
 
Thank you so much!!!!

I haven't checked all the tabs but it seems to be working. You are awesome!
 
Back
Top