How to Delete Rows in Excel Worksheet based on a Criteria

前端 未结 4 1625
抹茶落季
抹茶落季 2021-01-25 11:19

I have an excel workbook, in worksheet1 in Column A, IF the value of that column = ERR I want it to be deleted (the entire row), how is that possible?

PS: keep in mind t

4条回答
  •  暖寄归人
    2021-01-25 12:02

    Fastest method:

    Sub DeleteUsingAutoFilter()
    
        Application.ScreenUpdating = False
    
        With ActiveSheet
            .AutoFilterMode = False
    
            .Columns("A").AutoFilter Field:=1, Criteria1:="ERR"
    
            .AutoFilter.Range.Offset(1, 0).EntireRow.Delete        
    
            .AutoFilterMode = False
        End With
    
        Application.ScreenUpdating = True
    
    End Sub
    

    Second fastest method (lots of variations to this one too):

    Sub DeleteWithFind()
        Dim rFound As Range, rDelete As Range
        Dim sAddress As String
    
        Application.ScreenUpdating = False
    
        With Columns("A")
            Set rFound = .Find(What:="ERR", After:=.Resize(1, 1), SearchOrder:=xlByRows)
    
            If Not rFound Is Nothing Then
                Set rDelete = rFound                
                Do
                    Set rDelete = Union(rDelete, rFound)
                    Set rFound = .FindNext(rFound)
                Loop While rFound.Row > rDelete.Row                
            End If
    
            If Not rDelete Is Nothing Then rDelete.EntireRow.Delete
    
        End With
    
        Application.ScreenUpdating = True
    
    End Sub
    

    Autofilter method for multiple sheets:

    Sub DeleteUsingAutoFilter()
        Dim vSheets As Variant
        Dim wsLoop As Worksheet
    
        Application.ScreenUpdating = False
    
        '// Define worksheet names here
        vSheets = Array("Sheet1", "Sheet2")
    
        For Each wsLoop In Sheets(vSheets)
    
             With wsLoop
                 .AutoFilterMode = False
    
                 .Columns("A").AutoFilter Field:=1, Criteria1:="ERR"
    
                 .AutoFilter.Range.Offset(1, 0).EntireRow.Delete
    
                 .AutoFilterMode = False
             End With
    
        Next wsLoop
    
        Application.ScreenUpdating = True
    
    End Sub
    

提交回复
热议问题