Clear all Tables from All Sheets

jamesmev

Board Regular
Joined
Apr 9, 2015
Messages
233
Office Version
  1. 365
Platform
  1. Windows
I am currently using the below:
VBA Code:
           With Sheets("MY").ListObjects("MY")
        
        'Check If any data exists in the table
        If Not .DataBodyRange Is Nothing Then
            'Clear Content from the table
            .DataBodyRange.ClearContents
        End If
        
    End With
    
End Sub

To clear the table of the sheet "MY" and the Table "MY".

However, I am wanting to clear all Sheets and all tables but leaving the headers.
without having to do it for every sheet/table.
Any advice?
 

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
For example

VBA Code:
Sub jec()
 Dim sh As Worksheet, lo As ListObject
 For Each sh In ThisWorkbook.Sheets
    For Each lo In sh.ListObjects
       If Not lo.DataBodyRange Is Nothing Then lo.DataBodyRange.Delete
    Next
 Next
End Sub
 
Upvote 0
Solution
For example

VBA Code:
Sub jec()
 Dim sh As Worksheet, lo As ListObject
 For Each sh In ThisWorkbook.Sheets
    For Each lo In sh.ListObjects
       If Not lo.DataBodyRange Is Nothing Then lo.DataBodyRange.Delete
    Next
 Next
End Sub
Thank you! This is exactly what I was needing. Super Results
 
Upvote 0
For example

VBA Code:
Sub jec()
 Dim sh As Worksheet, lo As ListObject
 For Each sh In ThisWorkbook.Sheets
    For Each lo In sh.ListObjects
       If Not lo.DataBodyRange Is Nothing Then lo.DataBodyRange.Delete
    Next
 Next
End Sub
I found one Table that i need to save that can't be deleted each time..
Any Solutions? Table Name MatVariable
 
Upvote 0
I found one Table that i need to save that can't be deleted each time..
Any Solutions? Table Name MatVariable
Try this modicification to JEC's code:
VBA Code:
Sub jec()
 Dim sh As Worksheet, lo As ListObject
 For Each sh In ThisWorkbook.Sheets
    For Each lo In sh.ListObjects
       If (Not lo.DataBodyRange Is Nothing) And (lo.Name <> "MatVariable") Then lo.DataBodyRange.Delete
    Next
 Next
End Sub
 
Upvote 0
Try this modicification to JEC's code:
VBA Code:
Sub jec()
 Dim sh As Worksheet, lo As ListObject
 For Each sh In ThisWorkbook.Sheets
    For Each lo In sh.ListObjects
       If (Not lo.DataBodyRange Is Nothing) And (lo.Name <> "MatVariable") Then lo.DataBodyRange.Delete
    Next
 Next
End Sub
Thank you worked perfectly.
 
Upvote 0
You are welcome.

I am re-marking JEC's reply as the solution, as his solution did indeed answer your original question.
 
Upvote 0

Forum statistics

Threads
1,216,077
Messages
6,128,673
Members
449,463
Latest member
Jojomen56

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