Home > Software design >  Removing Excel rows by search criteria
Removing Excel rows by search criteria

Time:05-22

I am trying to remove Excel rows which have number below integer 5 in the cells of column 13 by VBA. The code simply filters the rows by a search criteria, then deletes the unwanted rows.

'1. Apply Filter
  ws.Range("A5:O202").AutoFilter Field:=13, Criteria1:="<5"
  
  '2. Delete Rows
  Application.DisplayAlerts = False
    ws.Range("A5:O202").SpecialCells(xlCellTypeVisible).Delete
  Application.DisplayAlerts = True
  
  '3. Clear Filter
  On Error Resume Next
    ws.ShowAllData
  On Error GoTo 0

To get it going, my code specifies a static range to accommodate different table lengths up to 202 rows, however can I change this so that the range sets itself to one before the first empty cell it discovers in column 13?

CodePudding user response:

Sub t()
Dim t As Long
Dim ws As Worksheet
    Set ws = ActiveSheet
    t = 1
    Do Until IsEmpty(ws.Cells(t, 13))
        If ws.Cells(t, 13) < 5 Then
            ws.Rows(t).Delete
        Else
            t = t   1
        End If
    Loop
End Sub
  • Related