Excel 2010 - Check if Table Already exists

mmetzinger

Board Regular
Joined
Dec 30, 2010
Messages
61
OK, so I have the following code in my project that creates a table for a find/copy/paste operation performed later on

Code:
    Range("A1", LastCG).Select
    Range("C6").Activate
    ActiveSheet.ListObjects.Add(xlSrcRange, Range("$A$1", LastCG), , xlYes). _
        Name = "Table1"
    Range("Table1[[#Headers],[Enroller Name]]").Select

This code works great but my issue is if the user runs the macro again without closing the original workbook the macro will blow up because it will try and create a table on top of the table that already exits.

So what I need to do is put something like a basic if/then statement to check and see if the table exists but I don't know how to call the table.

Can someone help

Example Result
Code:
If object.table1 exists then 

else
    Range("A1", LastCG).Select
    Range("C6").Activate
    ActiveSheet.ListObjects.Add(xlSrcRange, Range("$A$1", LastCG), , xlYes). _
        Name = "Table1"
    Range("Table1[[#Headers],[Enroller Name]]").Select
end if
 

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
OK, so I just found my own answer via another post. Here is what I came up with for code
Code:
'Check if Table1 exists and either create it or skip this step to prevent duplicate table error
    Dim ListObj As ListObject
    On Error Resume Next
    Set ListObj = ActiveSheet.ListObjects("Table1")
    On Error GoTo 0
    
    If ListObj Is Nothing Then
            'Establish the table for the find company operation
            Range("A1", LastCG).Select
            Range("C6").Activate
            ActiveSheet.ListObjects.Add(xlSrcRange, Range("$A$1", LastCG), , xlYes). _
                Name = "Table1"
    Else
        'If the table does exist clear filter from column C 
        ActiveSheet.ListObjects("Table1").Range.AutoFilter Field:=3
    End If

'Step into the table
    Range("Table1[[#Headers],[Enroller Name]]").Select
 
Upvote 0
As an alternative, here's a UDF that can be used:
Code:
Function TableExists(ws As Worksheet, tblNam As String) As Boolean
Dim oTbl As ListObject
For Each oTbl In ws.ListObjects
    If oTbl.Name = tblNam Then
        TableExists = True
        Exit Function
    End If
Next oTbl
TableExists = False
End Function
Sub test()
If TableExists(ActiveSheet, "Table1") Then
    MsgBox "Table1 Exists on sheet " & ActiveSheet.Name
Else
     MsgBox "Table1 Does Not Exist on sheet " & ActiveSheet.Name
End If
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,714
Messages
6,174,054
Members
452,542
Latest member
Bricklin

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top