简体   繁体   English

在Excel VBA中删除包含特定数据的完整行

[英]Delete complete rows in Excel VBA containing specific data

I am trying to delete complete row which contain the value "THD" in first cell. 我试图删除第一个单元格中包含值“ THD”的完整行。 The content could look like "THD_C_L10.0W5.0H15.0P7.5" but THD is always there. 内容可能看起来像“ THD_C_L10.0W5.0H15.0P7.5”,但THD始终存在。

I tried it with following loop: 我尝试了以下循环:

For i = 1 To ActiveSheet.UsedRange.Rows.Count
    If InStr(1, LCase(Tabelle2.Cells(i, 1)), "THD") <> 0 Then
        Rows(i).EntireRow.Delete
    End If
Next

But unfortunately its not working. 但不幸的是它无法正常工作。 Any ideas? 有任何想法吗?

When you delete rows you have to start your loop from last element and direct into first. 删除行时,您必须从最后一个元素开始循环并直接进入第一个元素。 Therefore, instead of: 因此,代替:

For i = 1 To ActiveSheet.UsedRange.Rows.Count

you should have something like this: 你应该有这样的东西:

For i = ActiveSheet.UsedRange.Rows.Count to 1 Step -1

One additional tip which shouldn't affect your current sub- InStr has additional parameter which you could use. 不应影响您当前的子InStr另一种技巧是可以使用其他参数。 This line is equivalent to yours one: 此行与您的行等效:

If InStr(1, Tabelle2.Cells(i, 1), "THD",vbTextCompare) <> 0 Then

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

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