vba for autofilter with three criteria

littlepete

Well-known Member
Joined
Mar 26, 2015
Messages
503
Office Version
  1. 365
Platform
  1. Windows
hello all...


as always, i think it will be easy to adjust something...
I had to criteria in my filters, i'm adding a third one:

VBA Code:
     If antwoord <> "55" Then
            ActiveSheet.Range("rnggebchr").AutoFilter Field:=18, Criteria1:=antwoord & "*", Operator:=xlAnd
            ActiveSheet.Range("rnggebchr").AutoFilter Field:=18, Criteria2:="<>*0100*", Operator:=xlAnd
            ActiveSheet.Range("rnggebchr").AutoFilter Field:=18, Criteria3:="<>*0101*", Operator:=xlAnd
        Else

I have no idea what could be wrong: the "<> 0101" is the new one, the two first combined worked well...
enjoy !!!
 

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
hello all...


as always, i think it will be easy to adjust something...
I had to criteria in my filters, i'm adding a third one:

VBA Code:
     If antwoord <> "55" Then
            ActiveSheet.Range("rnggebchr").AutoFilter Field:=18, Criteria1:=antwoord & "*", Operator:=xlAnd
            ActiveSheet.Range("rnggebchr").AutoFilter Field:=18, Criteria2:="<>*0100*", Operator:=xlAnd
            ActiveSheet.Range("rnggebchr").AutoFilter Field:=18, Criteria3:="<>*0101*", Operator:=xlAnd
        Else

I have no idea what could be wrong: the "<> 0101" is the new one, the two first combined worked well...
enjoy !!!
Could you provide a sample of your data?
 
Upvote 0
You are using wildcards ("*") it effectively uses the custom filter dialogue box which only allows 2 criteria.
You will need to use a helper column or Advanced Filter.


1641360016747.png
 
Upvote 0
@Alex Blakenburg you are correct in your advice to @littlepete - no more than 2 filter conditions using wildcards. I've always found this limitation annoying, so as well as the 2 options you outlined in post #3, there is another alternative I use in these cases that has no limit to the number of wildcard conditions.

Basically, you create a dictionary where the keys are the values in the column you want to apply the multiple wildcard conditions to. Then you loop through the array of keys - removing those that don't meet the conditions you want (using wildcards where necessary). You then filter the column using the values that remain as an array.

With the OP's example, and without knowing what range "rnggebchr" refers to, or what the value of "antwoord" is, I can only show a demonstration using assumptions, being:
1. the range incorporates columns A:R (1 to 18) with headers in row 1
2. antwoord = 50 (I had to use some value to test the code)

VBA Code:
Option Explicit
Sub LittlePete()
    Dim d As Object, c As Range, tmp As String, x, i As Long, ws As Worksheet
    Dim antwoord As String: antwoord = 50   '<~~No idea what 'antwoord' is
    Set ws = ActiveSheet
    Set d = CreateObject("scripting.dictionary")
    x = Application.Transpose(Range("R2", Cells(Rows.Count, "R").End(xlUp)))
  
    For i = 1 To UBound(x, 1)
        d(x(i)) = 1
    Next
  
    For Each c In ws.Range("R2", Cells(Rows.Count, "R").End(xlUp))
    tmp = c.Value
        If d.exists(tmp) And Not (tmp) Like antwoord & "*" Then d.Remove (tmp)  '<~~ 1
        If d.exists(tmp) And (tmp) Like "*0100*" Then d.Remove (tmp)            '<~~ 2
        If d.exists(tmp) And (tmp) Like "*0101*" Then d.Remove (tmp)            '<~~ 3
    Next
    
    With ws.Range("rnggebchr")              '<~~ assumes the named range is columns A:R
        .AutoFilter 18, Array(d.keys), 7
    End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,523
Messages
6,120,031
Members
448,940
Latest member
mdusw

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