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?
 

Excel Facts

How to change case of text in Excel?
Use =UPPER() for upper case, =LOWER() for lower case, and =PROPER() for proper case. PROPER won't capitalize second c in Mccartney
How about
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.UsedRange.Offset(1).SpecialCells _
          (xlCellTypeConstants, 23).ClearContents
        On Error GoTo 0
    Next
    Set wks = Nothing
End Sub
 
Upvote 0
I tried that. It did the same thing: cleared data and table headings, changing them to column 1, column 2, column 3, etc. rather than the names I had entered in.
 
Upvote 0
What row are your heading on?
 
Upvote 0
Change of plan, If you have 1 table per sheet try
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
        Set Tbl = wks.ListObjects(1)
        Tbl.DataBodyRange.SpecialCells _
          (xlCellTypeConstants, 23).ClearContents
        On Error GoTo 0
    Next
    Set wks = Nothing
End Sub
 
Upvote 0
I didn't try that macro because I have two tables on each sheet.

Headings are on row 3 and row 27.
 
Upvote 0
I didn't try that macro because I have two tables on each sheet.

Headings are on row 3 and row 27.

Are they real Excel Table objects or just normal ranges of cells on a sheet that you think of as being a table? If just normal ranges, what are the column extents of each table?
 
Upvote 0
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

Forum statistics

Threads
1,214,415
Messages
6,119,381
Members
448,888
Latest member
Arle8907

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