Using VBA to create wildcard filter for multiple criteria

MisterBeee

New Member
Joined
May 3, 2018
Messages
1
Hey guys, I'm new to using VBA, but I now know autofilter will only allow 2 sets of criteria for searching, and will not work w/wildcards (*). Here's the array function I tried to use that obviously didn't work.

ActiveSheet.Range("$A$10").CurrentRegion.AutoFilter Field:=2, Criteria1:= _
Array("1.3.0704.*", "1.4.0704.*", "1.4.0753.*", "1.4.0755.*"), Operator:=xlFilterValues

Can someone help me with the right code that will allow me to use wildcards to filter on multiple criteria? Thanks.
 

Excel Facts

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.
Try...

Code:
Option Explicit

Sub test()


    Dim dicCriteria As Object
    Dim vData As Variant
    Dim i As Long
    
    Set dicCriteria = CreateObject("Scripting.Dictionary")
    dicCriteria.CompareMode = 1 'vbTextCompare


    With ActiveSheet
        If .FilterMode Then .AutoFilterMode = False
        With .Range("A10").CurrentRegion
            vData = .Columns(2).Cells.Value
            For i = 2 To UBound(vData, 1) 'exclude headers and start from the second row of data
                If Not dicCriteria.Exists(vData(i, 1)) Then
                    Select Case True
                        Case vData(i, 1) Like "1.3.0704.*"
                            dicCriteria(vData(i, 1)) = ""
                        Case vData(i, 1) Like "1.4.0704.*"
                            dicCriteria(vData(i, 1)) = ""
                        Case vData(i, 1) Like "1.4.0753.*"
                            dicCriteria(vData(i, 1)) = ""
                        Case vData(i, 1) Like "1.4.0755.*"
                            dicCriteria(vData(i, 1)) = ""
                    End Select
                End If
            Next i
            If dicCriteria.Count > 0 Then
                .AutoFilter field:=2, Criteria1:=dicCriteria.Keys, Operator:=xlFilterValues
            Else
                MsgBox "No records found.", vbInformation
            End If
        End With
    End With

    Set dicCriteria = Nothing

End Sub

Hope this helps!
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,825
Messages
6,181,189
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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