VBA: Trying to delete all but the first row of a table

rizzo93

Active Member
Joined
Jan 22, 2015
Messages
303
Office Version
  1. 365
I want to delete all rows in a table EXCEPT for the first row in the databodyrange. Using tbl.DataBodyRange.Rows.Count, I plan to delete that many -1.

The problem I'm having is figuring out how to select my starting row in the databodyrange down to the last row of the table.

I thought of using this code, but not sure how to select more than one row with it (if that's even possible):

VBA Code:
ActiveSheet.ListObjects("tbl").ListRows(3).Range.Select

Your recommendations, please?
 

Excel Facts

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.
Why leave the first row?
VBA Code:
With ActiveSheet.ListObjects(tbl")
    If .ListRows.Count > 0 Then
        .DataBodyRange.Delete
    End If
End With
 
Upvote 0
Hi jkpieterse, thanks for the code. Simple and efficient!

I want to leave the first row so that when I paste new data into the left-most cell of the databodyrange (as plain text), the table will remain intact in terms of formatting and grow as needed.
 
Upvote 0
Why leave the first row?
VBA Code:
With ActiveSheet.ListObjects(tbl")
    If .ListRows.Count > 0 Then
        .DataBodyRange.Delete
    End If
End With
My row count will become increasingly larger, which will increase the time to delete them when I have to reset my data. While your code works, is there a way of selecting the rows I know that need to be deleted?
 
Upvote 0
Suppose you want to paste into a table which you first want emptied:
VBA Code:
    With ActiveSheet.ListObjects("tbl")
        If .ListRows.Count > 0 Then
            .DataBodyRange.Delete
        End If
        'Code to copy goes here
        Range("C1:C3").Copy
        'Now we can paste
        .Range.Cells(1).Offset(1).PasteSpecial xlPasteValuesAndNumberFormats
    End With
 
Upvote 0
Another option
VBA Code:
With ActiveSheet.ListObjects("tbl").DataBodyRange
   If .Rows.Count > 1 Then .Offset(1).Resize(.Rows.Count - 1).Delete
End With
 
Upvote 0

Forum statistics

Threads
1,223,630
Messages
6,173,454
Members
452,514
Latest member
cjkelly15

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