简体   繁体   中英

excel vba delete entire row if there is 0 value in a column

I want to make a VBA code which automatically delete the entire row if the value in column 'D' is 0. im using this code but when I try to run it it gives me an error saying "Application Defined or Object-Defined error". anybody know why ? or is there any other way to do this ?

Sub deletezeros()
    Dim c As Range
    Dim searchrange As Range
    Dim i As Long

    Set searchrange = Worksheets("sheet2").Range("D2", ActiveSheet.Range("D123432").End(xlUp))

    For i = searchrange.Cells.Count To 1 Step -1
        Set c = searchrange.Cells(i)
        If c.Value = "0" Then c.EntireRow.Delete
    Next i
End Sub

Could you try something a more simple procedure? Like:

    Dim SH As Worksheet
    Set SH = ThisWorkbook.Sheets("sheet2")
    Dim LR As Long
    LR = SH.Columns(4).Find("*", searchorder:=xlByRows, LookIn:=xlValues, searchdirection:=xlPrevious).Row
   '-----
    For i = LR To 2 Step -1
        If SH.Cells(i, 4).Value = "0" Then
            SH.Cells(i, 4).EntireRow.Delete
        End If
    Next i

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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