简体   繁体   中英

VBA How to Delete Table Rows

I have a table which can vary in range (rows only) depending on the numbers of inputs (rows).

I've written a code to transfer the data from the table to my data base. That's working fine.

But, at the end of this script, I'd like to delete the table rows, expect the first and the second rows, where respectively, my headers, and my formula lives.

Bearing in mind that the total number of rows can vary, how can I delete all the table rows but the 2 first one?

FYI

The table range (headres and first row) is A18:D19

    Dim Cl As Range
For Each Cl In Sheets("Data Entry").Range("A19:A1000")
    If Cl.Value = "" Then Exit For 'Exits on first empty cell
    Sheets("Source Ingredients").Range(Cl.Value).Value = Cl.Offset(, 2).Value

Thanks in advance Greg

This works, where Table1 is the name of the table. If you aren't sure how to find this, click in the table, then select Table Design in the banner at the top, and the table name is on the top left under Properties.

Application.Range("Table1").ListObject.DataBodyRange.Offset(1, 0).Resize(Application.Range("Table1").ListObject.DataBodyRange.Rows.Count - 1, _
    Application.Range("Table1").ListObject.DataBodyRange.Columns.Count).Rows.Delete

With the address abbreviated using a With statement:

With Application.Range("Table1").ListObject.DataBodyRange
    .Offset(1, 0).Resize(.Rows.Count - 1, .Columns.Count).Rows.Delete
End With

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM