Vba Clearing Datarange columns across multiple sheets

Melimob

Active Member
Joined
Oct 16, 2011
Messages
396
Office Version
  1. 365
Hi

I have looked at this thread which was helpful but doesn't give me quite what I need..

https://www.mrexcel.com/forum/excel-questions/1002856-vba-clearing-specific-table-columns.html
I also found this useful and started to write the code but realised it was referencing rows and I need specific columns.
https://www.thespreadsheetguru.com/blog/2014/6/20/the-vba-guide-to-listobject-excel-tables

Basically, I need to clear out all the databody ranges across all my tables in the workbook EXCEPT leaving the formulas?

Is there an easy way to do this or do I have to specify which columns to leave alone for each table?

Many thanks
Meli
 

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
I've found the answer which works great except that it's also clearing the tables from my lkup sheet which I need to keep.

https://www.mrexcel.com/forum/excel...-all-data-except-formulas-table-headings.html

I tried to recreate the tables as at first it was only deleting the first table but now all on that sheet.

So I need something that will run this code for All Sheets EXcept sheet named 'lkups'.

Code:
Sub ClearAllButFormulas()
    
    RemoveFiltersUnhide
        
    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

many thanks
 
Upvote 0
ok I found the answer however it still was looking at my LKUPs sheet but I recreated all the tables and so it gets rid of one I don't need. Would like to know how to stop it reading this sheet tho?

Code:
Sub ClearAllButFormulas()
    
    RemoveFiltersUnhide


  Dim ws As Worksheet
  Dim wks As Worksheet
  Dim Tbl As ListObject


For Each ws In ThisWorkbook.Worksheets
  If ws.Name <> "LKUPs" Then
  
    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
        
  End If


Next ws


End Sub
 
Upvote 0

Forum statistics

Threads
1,223,888
Messages
6,175,208
Members
452,618
Latest member
Tam84

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