简体   繁体   English

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

[英]What is the bug with these VBA code?

My Task is I have to delete the entire Row if The K column Has zero value in it.First thing came to my mind is looping But i have 4000 rows and 1000 rows may have K value as zero. 我的任务是如果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

I believe People say Its Time taking Process as it is looping. 我相信人们会说它正在花时间进行循环。 Thats why I thought better go for next methods and People also told me 'Find' is much faster than looping. 这就是为什么我认为最好使用下一个方法的原因,《 People》还告诉我“查找”比循环快得多。 So I have used the below code That I found when I was googling 所以我使用了我在谷歌搜索时发现的以下代码

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

But I found That the above code, it is also deleting the row if the k column has 10 or 20 Value in it. 但是我发现上面的代码,如果k列中有10或20值,它也会删除该行。 I mean if the Digit contains zero then its deleting 我的意思是如果数字包含零,则将其删除

example . 例子

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

Whats wrong with these code? 这些代码有什么问题? These code is too fast than looping I wanted to use but I found bug with it. 这些代码比我想使用的循环速度快,但发现了错误。

Please explain the reason to the bug. 请解释该错误的原因。 And any help to other method which is faster in deleting the rows if it has zero value in K column other than looping or (might be also looping should be too fast )? 并且对其他方法有什么帮助,如果该方法在K列中具有零值而不是循环,则可以更快地删除行(循环也可能太快了)? That would be greatly appreciated. 这将不胜感激。 Thanks 谢谢

Not is bug, add this parameter in find method 不是错误,请在find方法中添加此参数

LookAt:= xlwhole LookAt:= xlwhole

To use filter do 要使用过滤器

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM