Convert Table to normal range, table name unknown

Rowland Hamilton

Active Member
Joined
Nov 13, 2009
Messages
250
Folks:

How do I use this formula in excel vba...:

Code:
ActiveSheet.ListObjects("Table_Dyna_1").Unlist
...If I don't know the name of the table, or the table name will change?

Like maybe today the table name is "Table_Dyna_1" but later it could be "Table_Dyna_1_1" or "Table_Dyna_2" (probably because the original download is in the background or something). And I'm not sure of all possible variations on the table name.

I tried a wildcard asterix, but that didn't work:

Code:
ActiveSheet.ListObjects("Table_Dyna*").Unlist

I will perform the "convert to range" command on a freshly imported, selected and active table.

Thank you - Rowland
 

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.
Lets say you have a 4 tables named "Table_Dyna_i" where i is a numerical digit form 1 to 4 then code will be:
Code:
For i = 1 To 4
    ActiveSheet.ListObjects("Table_Dyna_" & i).Unlist
Next i
I can be wrong but you must have some info about tables' name.
 
Upvote 0
Thanks for your quick response. Doesn't work probably because only one of the tables in the array actually exists, we just don't which of the names is correct. Probably need to Dim a tablename and find it with this:

Code:
Sub FindAllTablesOnSheet()
    Dim oSh As Worksheet
    Dim oLo As ListObject
    Set oSh = ActiveSheet
    For Each oLo In oSh.ListObjects
        Application.Goto oLo.Range
        MsgBox "Table found: " & oLo.Name & ", " & oLo.Range.Address
    Next
End Sub

Just one table so don't know if I need loop but not sure how to run withoug the loop.

Rowland
 
Upvote 0
If there is only one, maybe just
Code:
ActiveSheet.ListObjects(1).Unlist
 
Upvote 0
Thanks. I just solved it with all this complicated formula:
Code:
Sub FindAllTablesOnSheet()
    Dim oSh As Worksheet
    Dim oLo As ListObject
    Dim MyTable As Variant
    Set oSh = ActiveSheet
    For Each oLo In oSh.ListObjects
        Application.Goto oLo.Range
        'MsgBox "Table found: " & oLo.Name & ", " & oLo.Range.Address
        MyTable = oLo.Name
        'MsgBox MyTable
        oSh.ListObjects(MyTable).Unlist
    Next
End Sub
BUt yours is 1 line of code with no variable setting, I hope it works.
-Rowland
 
Upvote 0
Thanks for your quick response. Doesn't work probably because only one of the tables in the array actually exists, we just don't which of the names is correct. Probably need to Dim a tablename and find it with this:

Code:
Sub FindAllTablesOnSheet()
    Dim oSh As Worksheet
    Dim oLo As ListObject
    Set oSh = ActiveSheet
    For Each oLo In oSh.ListObjects
        Application.Goto oLo.Range
        MsgBox "Table found: " & oLo.Name & ", " & oLo.Range.Address
    Next
End Sub

Just one table so don't know if I need loop but not sure how to run withoug the loop.

Rowland

How about this:

Code:
Sub FindAllTablesOnSheet()
    Dim oSh As Worksheet
    Dim oLo As ListObject
    Set oSh = ActiveSheet
    For Each oLo In oSh.ListObjects
        Application.Goto oLo.Range
        MsgBox "Table found: " & oLo.Name & ", " & oLo.Range.Address
        [COLOR="DarkOrchid"]ActiveSheet.ListObjects(oLo.Name).Unlist[/COLOR]
    Next
End Sub
 
Upvote 0
shg: Awesome, it works. I only have 1 table so I just need your 1 command. Thank you -Rowland:
Code:
Sub table_convert()
ActiveSheet.ListObjects(1).Unlist
End Sub
 
Upvote 0
P.Holko: You're right, just needed to use the variables already defined. If I need to convert all worksheet lists, I can use this:

Code:
Sub FindAllTablesOnSheetUnlistThem()
    Dim oSh As Worksheet
    Dim oLo As ListObject
    Set oSh = ActiveSheet
    For Each oLo In oSh.ListObjects
        Application.Goto oLo.Range
        'MsgBox "Table found: " & oLo.Name & ", " & oLo.Range.Address
        oSh.ListObjects(oLo.Name).Unlist
    Next
End Sub
 
Upvote 0
You're welcome. P.Holko's code is indeed more versatile.
 
Upvote 0
if you want to reduce lines of code, try procedure below.
As probably know this code will works with worksheet with unknown number of lists
Code:
Sub FindAllTablesOnSheet()
    Dim oLo As ListObject
    For Each oLo In ActiveSheet.ListObjects
        ActiveSheet.ListObjects(oLo.Name).Unlist
    Next
End Sub
regards
 
Upvote 0

Forum statistics

Threads
1,224,598
Messages
6,179,818
Members
452,946
Latest member
JoseDavid

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