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

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
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,224,818
Messages
6,181,151
Members
453,021
Latest member
Justyna P

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