VBA - autofilter table when criteria is not in an array

auggie24

New Member
Joined
Sep 11, 2018
Messages
7
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
I am working on automating a report, and need to set filters for the consumers. I need to filter out items from some columns, and any other data needs to remain.

Code:
    Lastrow& = Range("A:C").Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
    ActiveSheet.Range("$A$1:$AF" & Lastrow).AutoFilter Field:=20, Criteria1:=Array( _
        "ZTAC", "ZTAE", "ZTAN", "ZTAQ", "ZTNN"), Operator:=xlFilterValues
    ActiveSheet.Range("$A$1:$AF & Lastrow").AutoFilter Field:=28, Criteria1:=Array( _
        "123456", "234567", "345678", "456789", "567890"), Operator:=xlFilterValues

In both of these filters, I need to exclude an array, rather than include.

For the first set of data, I need to exclude "ZMBS", "ZRBO", and "ZTAD".

For the second set of data, I need to exclude "987654", "876543", "765432", and "654321"

Also, the exclude criteria might not always been in the dataset, so I need an error condition to allow the filter code to exclude the criteria that is present, and ignore if the criteria is not present, so it doesn't bug out on me.

Thanks!
 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().

Forum statistics

Threads
1,214,920
Messages
6,122,272
Members
449,075
Latest member
staticfluids

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