简体   繁体   English

VBA 如何删除表行

[英]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?请记住,总行数可能会有所不同,如何删除除第一个 2 行之外的所有表行?

FYI供参考

The table range (headres and first row) is A18:D19表格范围(标题和第一行)是 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.这有效,其中 Table1 是表的名称。 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.如果您不确定如何找到它,请单击表格,然后单击顶部横幅中的 select 表格设计,表格名称位于左上方的属性下。

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 语句缩写的地址:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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