How to Delete Rows in Excel Worksheet based on a Criteria

前端 未结 4 1624
抹茶落季
抹茶落季 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:03

    Using an autofilter either manually or with VBA (as below) is a very efficient way to remove rows

    The code below

    1. Works on the entire usedrange, ie will handle blanks
    2. Can be readily adpated to other sheets by changing strSheets = Array(1, 4). ie this code currently runs on the first and fourth sheets

       Option Explicit
      
      
      Sub KillErr()
      Dim ws As Worksheet
      Dim lRow As Long
      Dim lngCol As Long
      Dim rng1 As Range
      Dim strSheets()
      Dim strws As Variant
      strSheets = Array(1, 4)
      For Each strws In strSheets
          Set ws = Sheets(strws)
          lRow = ws.Cells.Find("*", , xlValues, , xlByRows, xlPrevious).Row
          lngCol = ws.Cells.Find("*", , xlValues, , xlByColumns, xlPrevious).Column
          Application.ScreenUpdating = False
          ws.Rows(1).Insert
          Set rng1 = ws.Range(ws.Cells(1, lngCol), ws.Cells(lRow + 1, lngCol))
          With rng1.Offset(0, 1)
              .FormulaR1C1 = "=RC1=""ERR"""
              .AutoFilter Field:=1, Criteria1:="TRUE"
              .EntireRow.Delete
              On Error Resume Next
              .EntireColumn.Delete
              On Error GoTo 0
          End With
      Next
      Application.ScreenUpdating = True
      End Sub
      

提交回复
热议问题