Macro to delete all data except formulas and table headings

dsrt16

Board Regular
Joined
Jun 18, 2005
Messages
208
I created a clear all button on a sheet that needs to be cleared at the end of the year, ready for use at the start of the next year. The macro worked, but it also cleared out the table headings. (the names of each column)

This was the macro I used:

Code:
Sub ClearAllButFormulas()
    Dim wks As Worksheet

    For Each wks In Worksheets
        'ignore errors in case there is only formulas
        On Error Resume Next
        wks.Cells.SpecialCells _
          (xlCellTypeConstants, 23).ClearContents
        On Error GoTo 0
    Next
    Set wks = Nothing
End Sub

What do I need to add to it to prevent it from deleting the table headings?
 
Fluff

I added a button and assigned it the macro. It worked again. Then I reformatted cells outside of the tables, changing their background color, and when I tried the macro again, it did not work. It said error: no cells were found. So I undid all the formatting it, and it worked again.

But then I added a text box label to the sheet, and when I ran the macro, I got the error again. I deleted the text box labels I had added to see if they were the problem, but the macro is still giving me that error.

I don't understand. I only changed cells outside of the tables, so I am not sure why that should affect the macro. When I reversed my changes the first time, the macro worked again. When I reversed the most recent changes, the macro still didn't work. Any ideas?
 
Last edited:
Upvote 0

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
It highlights this line when I debug:

Code:
Tbl.DataBodyRange.SpecialCells _
             (xlCellTypeConstants, 23).ClearContents

I just don't understand why it worked at first, but not now. Nothing inside the tables changed.
 
Upvote 0
So I opened an older version of the workbook, added the macro, tested it, and it worked. Then as a test, I pressed the clear all button again, and it went into debug mode (since there was no data to clear), but I thought the macro was set to ignore all errors in case all that was left was formulas. I then added the data and tried again, but it didn't work since it was an already broken macro in debug mode. I stepped out of it, but it still didn't work. So even though there is now data to clear, the macro won't work after it had an error before.
 
Upvote 0
Try this
Code:
Sub ClearAllButFormulas()
    Dim wks As Worksheet
    Dim Tbl As ListObject

    For Each wks In Worksheets
        'ignore errors in case there is only formulas
        For Each Tbl In wks.ListObjects
            On Error Resume Next
            Tbl.DataBodyRange.SpecialCells _
               (xlCellTypeConstants, 23).ClearContents
            On Error GoTo 0
        Next Tbl
    Next
    Set wks = Nothing
End Sub
 
Upvote 0
This is exactly what I am looking for thank you

Ok try this
Code:
Sub ClearAllButFormulas()
    Dim wks As Worksheet
    Dim Tbl As ListObject

    For Each wks In Worksheets
        'ignore errors in case there is only formulas
        On Error Resume Next
        For Each Tbl In wks.ListObjects
           Tbl.DataBodyRange.SpecialCells _
             (xlCellTypeConstants, 23).ClearContents
           On Error GoTo 0
        Next Tbl
    Next
    Set wks = Nothing
End Sub
 
Upvote 0
Glad it worked for you and thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,224,827
Messages
6,181,195
Members
453,021
Latest member
pingpong7117

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