[英]Delete all cells in a range if a specific value is found on a cell - VBA
I need to perform the below action:我需要执行以下操作:
From a data range in an excel sheet in need to delete all the values if a given condition is found in a specific column, here is the code i wrote: (delete all the rows that have FLWE or FW in the first cell)如果在特定列中找到给定条件,则需要从 excel 工作表中的数据范围中删除所有值,这是我编写的代码:(删除第一个单元格中具有 FLWE 或 FW 的所有行)
Sub cancellaflw()
Dim RowFINE, LastRowDati2 As Long
With ThisWorkbook.Sheets("Dati")
RowFINE = 1
LastRowDati2 = ThisWorkbook.Sheets("Dati").Cells(Rows.count, 1).End(xlUp).Row
For RowFINE = 1 To LastRowDati2
If .Cells(RowFINE, 1) Like "FLWE*" Or .Cells(RowFINE, 1) Like "FW*" Then
.Range("A" & RowFINE & ":X" & RowFINE).Delete
RowFINE = RowFINE - 1
End If
Next RowFINE
End With
End Sub
Unfortunately this is not working, any idea on the reason why?不幸的是,这不起作用,知道为什么吗?
Thanks, Matteo谢谢,马特奥
You have made a very common newbie mistake.你犯了一个很常见的新手错误。 You need to change
你需要改变
For RowFINE = 1 To LastRowDati2
to到
For RowFINE = LastRowDati2 to 1 step -1
This means that you are now not changing the range over which you are iterating.这意味着您现在没有更改迭代的范围。 With your current code when you delete row 3, then row 4 automatically becomes row 3, but Rowfine doesn't know this has happened.
使用您当前的代码,当您删除第 3 行时,第 4 行会自动变为第 3 行,但 Rowfine 不知道发生了这种情况。 So increments to 4 so you have effectively skipped over a row.
所以增加到 4 所以你已经有效地跳过了一行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.