How To Use Or Delete A Table (ListObject)?

CaliKidd

Board Regular
Joined
Feb 16, 2011
Messages
173
I am using Excel 2007 and I created a table (i.e., listobject) using the macro recorder as follows:
Code:
ActiveSheet.ListObjects.Add(xlSrcRange, Range("$A$3:$J$50"), , xlYes).Name = "MyData"

Questions:
1. How do I delete this table via VBA?
2. How would I check to see if this table already exists so it follows this logic:

If "table does not exists" then
'create it (using the line of code above)
End If
 

Excel Facts

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.
To delete a table and its data, try...

Code:
ActiveSheet.ListObjects("MyData").Delete

To delete a table without losing its data or table formatting, try...

Code:
ActiveSheet.ListObjects("MyData").Unlist

In the following code, if a table named "MyData" does not exist, it creates one...

Code:
Option Explicit

Sub test()

    Dim ListObj As ListObject
    
    On Error Resume Next
    Set ListObj = ActiveSheet.ListObjects("MyData")
    On Error GoTo 0
    
    If ListObj Is Nothing Then
        Set ListObj = ActiveSheet.ListObjects.Add(xlSrcRange, Range("$A$3:$J$50"), , xlYes)
        ListObj.Name = "MyData"
    End If

End Sub
 
Upvote 0
So, call me a stickler. #1. I don't like code that relies upon generating an error to work, and #2. I don't like magic "numbers" (or strings). So, if you're like me, use this code instead:

Code:
Option Explicit

Sub test()

    Dim ListObj As ListObject
    Dim MyName As String
    Dim MyRangeString As String
    Dim MyListExists As Boolean

    MyName = "MyData"
    MyRangeString = "$A$3:$J$50"

    MyListExists = False
    For Each ListObj In ActiveSheet.ListObjects
       
        If ListObj.Name = MyName Then MyListExists = True
        
    Next ListObj
    
    If Not(MyListExists) Then
        Set ListObj = ActiveSheet.ListObjects.Add(xlSrcRange, Range(MyRangeString), , xlYes)
        ListObj.Name = MyName
    End If

End Sub

This doesn't rely on an error being generated to detect it doesn't exist.
This also has the advantage that you can change the variable "MyName" to check for other tables, or even loop through a set of names.
Same with MyRangeString, if your range changes (better would be to calculate it somehow)
 
Upvote 0

Forum statistics

Threads
1,221,493
Messages
6,160,139
Members
451,624
Latest member
TheWes

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