VBA to flatten tables (ListObjects) to ranges (XL2013)

AOB

Well-known Member
Joined
Dec 15, 2010
Messages
660
Office Version
  1. 365
  2. 2016
  3. 2013
Platform
  1. Windows
Hi everybody,

I need some code to convert all tables in a workbook to "flat" ranges (that is, retain the data, just get rid of the table structure around it)

I thought this would be fairly straightforward :

Code:
Public Sub FlattenTables()
    Dim sht As Worksheet
    Dim lso As ListObject
    For Each sht In ThisWorkbook.Sheets
        For Each lso In sht.ListObjects
            lso.Unlist
        Next lso
    Next sht
End Sub

But for some reason it throws a run-time error (1004) every time it tries to unlist the object?

Application-defined or object-defined error

My first instinct was that I couldn't unlist a listobject while it is held as a ListObject variable (i.e. by doing so, it is no longer a ListObject variable, thus conflict)

So then I tried just compiling a list of table names and then unlisting them individually outside the object scope like so :

Code:
ThisWorkbook.Sheets("SheetName").ListObjects("TableName").Unlist

But I just get the same error? :oops:

Any suggestions?
 
Last edited:

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.
Is the sheet protected?
 
Upvote 0
Weirdly, no - I thought it did? I'm prompted as normal :

Do you want to convert the table to a normal range?

But after clicking Yes, the table remains (and is still present in the Name Manager)
 
Upvote 0
Hi

I don't know what's happening, but the next things I'd do would be to

- check if it's some add-in or customization that's interfering with the conversion (execute excel in safe mode)

- see if it's some corruption in the file (try open and repair that file)


It can also happen that your file is corrupted beyond repair. In that case salvage what you can :(


... also wait some more time for cleverer suggestions.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,646
Messages
6,120,717
Members
448,985
Latest member
chocbudda

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