【发布时间】:2021-07-02 15:38:51
【问题描述】:
我。做。不是。了解。
我正在尝试根据值删除行(目标位于第 11 列)。 此代码仅适用于我要删除的一半单元格,因此我必须运行它 10 次(从 520 到 260,然后是 130、65...)
我的代码看起来很简单,但我仍然无法弄清楚问题出在哪里......
感谢您的帮助
Dim Target As String, i As Integer
Target = "Réduction ligne"
lastrow = Cells(Rows.Count, 11).End(xlUp).Row
For i = 1 To lastrow
On Error Resume Next
If Cells(i, 11).Value = Target Then
Rows(i).EntireRow.Delete = True
End If
Next i
【问题讨论】:
-
你应该向后迭代。否则引用将丢失...更好/更快的方法是将要删除的行中的所有单元格放在(Union)范围中,然后立即删除其 EntireRow。
-
做一个反向循环
For i = lastrow to 1 Step -1 -
另外,您不需要循环来删除包含“Réduction ligne”的行。使用AUTOFILTER。它要快得多。
-
在 SO 上经常被回答,例如在...Delete entire row
-
哇,感谢您的快速回复,我是 vba 新手,所以我仍然无法优化我的宏