VBA autofilter using an array

mecerrato

Board Regular
Joined
Oct 5, 2015
Messages
184
Office Version
  1. 365
Platform
  1. Windows
I am using the below code to try and filter out these values but I don't know how to tell it to not equal these values.
My end goal is to show all values except the ones listed in the array, can anyone help me figure this out?

VBA Code:
     ActiveSheet.Range("$a$10:$cr$500").AutoFilter Field:=8, Criteria1:=Array( _
     "*HOME*", "*BOND*", "In Origination", "*FHA*"), Operator:=xlFilterValues
 

Excel Facts

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.
Unfortunately there isn't a simple way to exclude more than two items. You could try this code. I have assumed that it was deliberate that "In Origination" does not include asterisks like the others.
See if it does what you want.

VBA Code:
Sub AF_NotIncludingMany()
  Dim RX As Object, d As Object
  Dim a As Variant
  Dim i As Long
 
  Const WildcardExclusions As String = "HOME|BOND|FHA" '<- Add more if req'd. Note: "|" is not at start or end
  Const ExactExclusions As String = "|In Origination|XYZ|" '<- Add more if req'd. Note: "|" is at start and end
 
  Set d = CreateObject("Scripting.Dictionary")
  d.CompareMode = 1
  Set RX = CreateObject("VBScript.RegExp")
  RX.IgnoreCase = True
  RX.Pattern = WildcardExclusions
  With Range("$A$10:$CR$500")
    a = .Columns(8).Value
    For i = 2 To UBound(a)
      If InStr(1, ExactExclusions, "|" & a(i, 1) & "|", vbTextCompare) = 0 And Not RX.Test(a(i, 1)) Then d(a(i, 1)) = 1
    Next i
    If d.Count > 0 Then .AutoFilter Field:=8, Criteria1:=d.Keys, Operator:=xlFilterValues
  End With
End Sub
 
Upvote 0
Solution
Unfortunately there isn't a simple way to exclude more than two items. You could try this code. I have assumed that it was deliberate that "In Origination" does not include asterisks like the others.
See if it does what you want.

VBA Code:
Sub AF_NotIncludingMany()
  Dim RX As Object, d As Object
  Dim a As Variant
  Dim i As Long
 
  Const WildcardExclusions As String = "HOME|BOND|FHA" '<- Add more if req'd. Note: "|" is not at start or end
  Const ExactExclusions As String = "|In Origination|XYZ|" '<- Add more if req'd. Note: "|" is at start and end
 
  Set d = CreateObject("Scripting.Dictionary")
  d.CompareMode = 1
  Set RX = CreateObject("VBScript.RegExp")
  RX.IgnoreCase = True
  RX.Pattern = WildcardExclusions
  With Range("$A$10:$CR$500")
    a = .Columns(8).Value
    For i = 2 To UBound(a)
      If InStr(1, ExactExclusions, "|" & a(i, 1) & "|", vbTextCompare) = 0 And Not RX.Test(a(i, 1)) Then d(a(i, 1)) = 1
    Next i
    If d.Count > 0 Then .AutoFilter Field:=8, Criteria1:=d.Keys, Operator:=xlFilterValues
  End With
End Sub
That is a beautiful solution to an annoying issue, thanks so much for doing this :) worked perfectly!
 
Upvote 0

Forum statistics

Threads
1,223,894
Messages
6,175,252
Members
452,623
Latest member
Techenthusiast

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