I'm using an autofilter delete macro to clear out unwanted rows without using loops. It works fine for one off number entries e.g "200". What I need to do is set the criteria to match any number beginning with 4, e.g. 400-499 but the wildcard "4*" does not work. There must be a simple solution to this but I'm missing it right now!
Here's the code:
Sub KeepOnly4xxRows()
'
'Filter rows with 4xx. Delete rows which do not contain 4xx.
Dim ws As Worksheet
Dim rng As Range
Dim lastRow As Long
Set ws = ActiveWorkbook.Sheets("4xx")
lastRow = ws.Range("B" & ws.Rows.Count).End(xlUp).Row
Set rng = ws.Range("B1:B" & lastRow)
' filter all non-4xx entries and delete all but header row.
With rng
.AutoFilter Field:=1, Criteria1:="<>4*"
.Offset(1, 0).SpecialCells(xlCellTypeVisible).EntireRow.Delete
End With
End Sub
Here's the code:
Sub KeepOnly4xxRows()
'
'Filter rows with 4xx. Delete rows which do not contain 4xx.
Dim ws As Worksheet
Dim rng As Range
Dim lastRow As Long
Set ws = ActiveWorkbook.Sheets("4xx")
lastRow = ws.Range("B" & ws.Rows.Count).End(xlUp).Row
Set rng = ws.Range("B1:B" & lastRow)
' filter all non-4xx entries and delete all but header row.
With rng
.AutoFilter Field:=1, Criteria1:="<>4*"
.Offset(1, 0).SpecialCells(xlCellTypeVisible).EntireRow.Delete
End With
End Sub