Excel VBA
我想基于cond删除所有行
看看声明
但代码不会删除所有具有ws.Cells(i, 6).Value = 0
Sub Clean()
For Each ws In Worksheets
' find Last Row
LastRow = ws.Cells(Rows.Count, 1).End(xlUp).Row
' loop over all rows
For i = 2 To LastRow ' start of the for loop
If ws.Cells(i, 6).Value = 0 Then
Rows(i).Delete
End If
Next i
Next ws
End Sub
答案 0 :(得分:1)
替换
For i = 2 To LastRow
带
For i = LastRow to 2 step -1
应该工作得更好。
答案 1 :(得分:0)
我发现你遗漏了一些重要的代码。
当您删除一行时,i
变量会自动跳过1行,并且不会验证删除行后的当前行。
Sub Clean()
For Each ws In Worksheets
' find Last Row
LastRow = ws.Cells(Rows.Count, 1).End(xlUp).Row
' loop over all rows
For i = 2 To LastRow ' start of the for loop
If ws.Cells(i, 6).Value = 0 Then
Rows(i).Delete
i = i - 1 ' force your code to review the same line again
End If
Next i
Next ws
End Sub
希望它有所帮助。