VBA Clearing Specific Table Columns

bs0d

Well-known Member
Joined
Dec 29, 2006
Messages
622
I'd like to clear specific columns in an Excel Table (in VBA). Currently using v2013.

.ClearContents will clear the entire table. I want to retain field names in the left-most column.

Is there a way to detect the the end of the table like a uBound() on an array and perhaps loop to clear DataBodyRange?
 

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
DataBodyRange will return a range and it should be straightforward to manipulate it to only clear the columns you want.

For example this will clear all columns bar the leftmost.
Code:
Dim lst As ListObject
Dim rng As Range

    Set lst = ActiveSheet.ListObjects("Table2")
    
    Set rng = lst.DataBodyRange
    
    rng.Offset(, 1).Resize(, rng.Columns.Count - 1).ClearContents
 
Upvote 0
DataBodyRange will return a range and it should be straightforward to manipulate it to only clear the columns you want.

For example this will clear all columns bar the leftmost.
Code:
Dim lst As ListObject
Dim rng As Range

    Set lst = ActiveSheet.ListObjects("Table2")
    
    Set rng = lst.DataBodyRange
    
    rng.Offset(, 1).Resize(, rng.Columns.Count - 1).ClearContents

This does the trick, Norie. Thanks.
 
Upvote 0
Can you "resize" the table to fit contents using the same/similar approach?
 
Upvote 0

Forum statistics

Threads
1,223,896
Messages
6,175,260
Members
452,627
Latest member
KitkatToby

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