这些VBA代码的错误是什么?

我的任务是我必须删除整个行,如果K列中有零值。首先我想到的是循环但我有4000行和1000行可能有K值为零。

Sub DeleteRowWithContents() Last = Cells(Rows.Count, "D").End(xlUp).Row For i = Last To 1 Step -1 If (Cells(i, "K").Value) = 0 Then Cells(i, "A").EntireRow.Delete End If Next i End Sub 

我相信人们说它的时间正在循环。 这就是为什么我认为更好的去接下来的方法,人们也告诉我,“查找”比循环要快得多。 所以我使用了我在Google上search时发现的下面的代码

 Sub Delete_zeros() Dim rCell As Range Dim strAddress As String Application.ScreenUpdating = False ThisWorkbook.Sheets(1).Activate With ActiveSheet.Columns("K") Set rCell = .Find(What:=0, LookIn:=xlValues, SearchOrder:=xlByColumns) If Not rCell Is Nothing Then Do strAddress = rCell.Address rCell.EntireRow.Delete Set rCell = .FindNext(Range(strAddress)) Loop Until rCell Is Nothing End If End With Application.ScreenUpdating = True End Sub 

但是我发现上面的代码,如果k列中有10或20的值,也是删除行。 我的意思是如果数字包含零则删除

例如。

  204 or 200 or 205 or 301 or 10 \ Its deleting all these rows 

这些代码有什么问题? 这些代码比我想要使用的循环太快,但是我发现它有问题。

请解释错误的原因。 和其他任何帮助删除行,如果它有K列零循环或(也可能循环应该是太快)零速度更快? 这将不胜感激。 谢谢

不是bug,在find方法中加上这个参数

LookAt:= xlwhole

要使用filter吗

 Sub FilterAndDelete() 'Developer by Bruno Leite 'http://officevb.com  Dim Sht As Worksheet  Dim FilterRange As Range   'Set your Sheet  Set Sht = ThisWorkbook.Sheets("Plan2")   'Verify if is Filter  If Sht.FilterMode Then   Sht.ShowAllData  End If   'Filter Column A with 0 at parameter  Sht.Range("A:A").AutoFilter Field:=1, Criteria1:="0"   'Define Range Of Visible cells without row title  Set FilterRange = Sht.Range("A1").CurrentRegion.Offset(1, 0).SpecialCells(xlCellTypeVisible)      Application.DisplayAlerts = False   FilterRange.Rows.Delete   Application.DisplayAlerts = True     Sht.ShowAllData End Sub