lRow = Cells(Rows.Count, "AI").End(xlUp).Row
For iCntr = 1 To lRow
If Cells(iCntr, 35) = 0 Then
Rows(iCntr).Delete
End If
Next
上述代码没有显示任何错误,但没有删除列(“AI”)单元格为0的整行。请帮助我使用正确版本的代码。
答案 0 :(得分:2)
你应该自下而上地工作:
lRow = Cells(Rows.Count, "AI").End(xlUp).Row
For iCntr = lRow To 1 Step -1
If Cells(iCntr, 35) = 0 Then
Rows(iCntr).Delete
End If
Next
答案 1 :(得分:1)
要删除所需的行,您无需遍历行。
您可以使用自动过滤功能一次删除所有行。
试一试......
Sub DeleteRows()
Dim lRow As Long
Application.ScreenUpdating = False
lRow = Cells(Rows.Count, "AI").End(xlUp).Row
ActiveSheet.AutoFilterMode = False
With Range("AI1:AI" & lRow)
.AutoFilter field:=1, Criteria1:=0
If .SpecialCells(xlCellTypeVisible).Cells.Count > 1 Then
Range("AI2:AI" & lRow).SpecialCells(xlCellTypeVisible).EntireRow.Delete
End If
.AutoFilter
End With
Application.ScreenUpdating = True
End Sub
答案 2 :(得分:1)
更快的解决方案是使用DelRng
对象,每次迭代时都符合条件,然后使用DelRng
函数将该行添加到Union
。
最后,您一次删除所有行。
尝试以下代码:
Dim DelRng As Range
With Sheets("Sheet1") ' modify to your sheet's name (allways qualify with the worksheet object)
lRow = .Cells(.Rows.Count, "AI").End(xlUp).Row
For iCntr = 1 To lRow
If .Cells(iCntr, 35) = 0 Then
If Not DelRng Is Nothing Then
Set DelRng = Application.Union(DelRng, .Rows(iCntr))
Else
Set DelRng = .Rows(iCntr)
End If
End If
Next
' delete the entire rows at once >> will save you a lot of time
DelRng.Delete
End With