EDIT: My variables contain wildcards. I saw something online that says that if I am using wildcards, the autofilter can contain only two conditions. Is this true? If so, this is
AutoFilter is limited to 2 criteria with wildcards (* or ?)
The 2 versions below will allow you to specify as many wildcards as you need
.
Version 1 - Loops through each wildcard applying the AutoFliter and combining the visible ranges
Option Explicit
Public Sub FilterRows3WildAF() '(Optional ByVal showAll As Boolean = False)
Const FILTER_COL = "A"
Const WILDCARDS = "Name Street Address Number" 'cell starts with these 4 words
Dim ws As Worksheet, wild As Variant, lr As Long, toShow As Range, itm As Variant
Set ws = ActiveSheet
wild = Split(WILDCARDS) 'will search for cells starting with: Name*, then Street*, etc
Application.ScreenUpdating = False
ws.Rows.Hidden = False
With ws.Range(ws.Cells(1), ws.Cells(ws.Rows.Count, FILTER_COL).End(xlUp))
lr = .Rows.Count
Set toShow = .Cells(lr + 1, FILTER_COL)
For Each itm In wild
.AutoFilter Field:=1, Criteria1:=itm & "*", Operator:=xlFilterValues
If .SpecialCells(xlCellTypeVisible).Cells.CountLarge > 1 Then
Set toShow = Union(toShow, .Offset(1).SpecialCells(xlCellTypeVisible))
End If
Next
.AutoFilter
.Rows.Hidden = True
toShow.EntireRow.Hidden = False
End With
Application.ScreenUpdating = True
End Sub
.
Version 2 - Loops through each cell, checking with InStr() if the wildcard exists
Public Sub FilterRows3WildInstr() '(Optional ByVal showAll As Boolean = False)
Const FILTER_COL = "A"
Const WILDCARDS = "Name Street Address Number" 'cell starts with these 4 words
Dim ws As Worksheet, wild As Variant, lr As Long, arr As Variant
Dim toHide As Range, r As Long, itm As Variant, found As Boolean
Set ws = ActiveSheet
wild = Split(WILDCARDS) 'will search for cells starting with: Name*, then Street*, etc
ws.Rows.Hidden = False
With ws.Range(ws.Cells(1), ws.Cells(ws.Rows.Count, FILTER_COL).End(xlUp))
lr = .Rows.Count
arr = .Value2
Set toHide = .Cells(lr + 1, FILTER_COL)
For r = 1 To UBound(arr)
For Each itm In wild
found = InStr(1, arr(r, 1), itm) > 0
If found Then Exit For
Next
If Not found Then Set toHide = Union(toHide, .Cells(r, FILTER_COL))
Next
toHide.EntireRow.Hidden = True: .Rows(lr + 1).Hidden = False
End With
End Sub