简体   繁体   中英

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. But it does not delete all of the rows where the value ends in "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!

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