Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
这些VBA代码的错误是什么?_Vba_Excel - Fatal编程技术网

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

这些VBA代码的错误是什么?,vba,excel,Vba,Excel,我的任务是,如果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

我的任务是,如果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
我相信人们会说这是一个耗时的过程,因为它是循环的。这就是为什么我认为最好采用下一种方法,人们还告诉我“查找”比循环快得多。所以我使用了我在谷歌搜索时发现的以下代码

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方法中添加此参数

看:=xlother

使用过滤器做什么

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

我认为这不是问题所在,因为它甚至可以与Not一起工作,但感谢Bruno:)另一种方法是使用过滤方法删除可见单元格。你认为过滤或查找哪个更快?我相信你是对的?