简体   繁体   English

如何在Excel VBA中找到以“net”结尾并删除行的单元格值

[英]How to locate a cell value that ends in “net” and delete row in Excel VBA

I have this procedure that is looking for any cell in Col D where the value ends in "net" then it will delete the entire row/rows. 我有这个程序正在寻找Col D中的任何单元格,其中值以“net”结尾,然后它将删除整个行/行。 But it does not delete all of the rows where the value ends in "net". 但它不会删除值以“net”结尾的所有行。 I am out of ideas. 我没有想法。

Sub DeleteRows_net()
Application.ScreenUpdating = False
Dim rng As Range

For Each rng In Range("D4:D2700")

If InStr(1, rng.Value, "net") > 0 Then
rng.EntireRow.Delete
End If

Next rng

End Sub

It is so common mistake... When you loop to delete you have to start from last element taking direction to first. 这是如此常见的错误......当你循环删除时,你必须从最后一个元素开始向第一个方向开始。 Change looping into: 将循环更改为:

Dim i%
For i=2700 to 4 Step -1
    If InStr(1, Cells(i, "D").Value, "net") > 0 Then
        Cells(i, "D").EntireRow.Delete
    End If
Next i

By the way... Do not forget to place Application.ScreenUpdating = True somewhere before end of procedure! 顺便说一下......不要忘记在Application.ScreenUpdating = True结束前的某个地方放置Application.ScreenUpdating = True

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

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