Filtering Out Desired Criteria and Deleting All Others

FrenchCelt

Board Regular
Joined
May 22, 2018
Messages
214
Office Version
  1. 365
Platform
  1. Windows
Hello,

I'm in the middle of coding a macro that will filter for various criteria in order to delete them from a report that has a lot of extraneous data. For example, take the code below:

VBA Code:
ActiveSheet.Range("A4:O50000").AutoFilter Field:=3, Criteria1:="PK BAG"
ActiveSheet.Range("A4:O50000").AutoFilter Field:=4, Criteria1:="SKIP"
On Error Resume Next
Range("A5:A50000").SpecialCells(xlCellTypeVisible).EntireRow.Delete
ActiveSheet.ShowAllData
    
ActiveSheet.Range("A4:O50000").AutoFilter Field:=3, Criteria1:="PK BAG"
ActiveSheet.Range("A4:O50000").AutoFilter Field:=4, Criteria1:="LQTY"
On Error Resume Next
Range("A5:A50000").SpecialCells(xlCellTypeVisible).EntireRow.Delete
ActiveSheet.ShowAllData

The only criteria I want to keep in Field 4 for category PK BAG is called GRABS, so I coded to have SKIP and LQTY deleted. What I would like to do for the sake of shortening the time to code this for a few dozen categories is to declare the criterion per category I want to keep and delete all others instead of listing one at a time all the criteria that should be deleted (e.g. if it's not GRABS then delete).

Any help would be greatly appreciated.
 

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!
How about
VBA Code:
Sub FrenchCelt()
With ActiveSheet
   .Range("A4:O50000").AutoFilter Field:=3, Criteria1:="PK BAG"
   .Range("A4:O50000").AutoFilter Field:=4, Criteria1:="<>GRABS"
   .AutoFilter.Range.Offset(1).EntireRow.Delete
   .ShowAllData
End With
End Sub
 
Upvote 0
You're welcome & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,215,831
Messages
6,127,139
Members
449,361
Latest member
VBquery757

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