2

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.

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. 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. 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 )? That would be greatly appreciated. Thanks

1 Answer 1

4

Not is bug, add this parameter in find method

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
Sign up to request clarification or add additional context in comments.

3 Comments

Not is aint the bug i think because Its working even with not But thanks Bruno :)
Other way is use filter method and delete visible cells.
Do you think which is faster filter or Find? I belieave filter right?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.