Auto Filter delete based on criteria

Jadgy1

New Member
Joined
Sep 15, 2015
Messages
8
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
 

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
Try...

Code:
.AutoFilter Field:=1, Criteria1:=">=400", Operator:=xlAnd, Criteria2:="<=499"

Hope this helps!
 
Upvote 0

Forum statistics

Threads
1,223,714
Messages
6,174,059
Members
452,542
Latest member
Bricklin

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top