vba代码在特定列的所有单元格中search0,如果find,则删除该行
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
上面提到的代码没有显示任何错误,但没有删除Column(“AI”)单元格为0的整个行。请帮助我使用正确的代码版本。
@Tim Williams的回答会解决你的问题,如果你想删除很多行,需要很长时间来处理它。
一个更快的解决scheme是使用DelRng
对象,每次迭代时,满足条件,使用Union
函数将该行添加到DelRng
。 最后,你一次删除所有的行。
试试下面的代码:
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
你应该从下往上工作:
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
要删除所需的行,您不需要遍历行。
您可以使用自动filter一次删除所有行。
试试这个…
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