Filter Question

Huey462

Board Regular
Joined
Jul 25, 2011
Messages
147
I have 2 fields that I would like to use to have the records displayed to only be those that match the criteria. Individually the filter works but when I try to combine them I get a Syntax error for a missing operator.

VBA Code:
Private Sub ApplyFilter()

'Entire Filter String
Dim FltCriteria As String
'Individual Section Strings
Dim FilterNo1Check As String
Dim FilterNo2Check As String

Forms!frmPhoneListing.FilterOn = False
FltCriteria = ""

If Me!FilterNo1 <> "" Then
    FilterNo1Check = Me!FilterNo1
    FltCriteria = " [PhContactMain]LIKE " & "'" & "*" & FilterNo1Check & "*" & "'"
End If

If Me!FilterNo2 <> "" Then
    FilterNo2Check = Me!FilterNo2
    If FltCriteria = "" Then
        FltCriteria = " [PhContactSub]LIKE " & "'" & "*" & FilterNo2Check & "*" & "'"
        Else
        FltCriteria = FltCriteria & " [PhContactSub]LIKE " & "'" & "*" & FilterNo2Check & "*" & "'"
    End If
End If

Forms!frmPhoneListing.Filter = FltCriteria
Forms!frmPhoneListing.FilterOn = True
End Sub
 

Excel Facts

Can you AutoAverage in Excel?
There is a drop-down next to the AutoSum symbol. Open the drop-down to choose AVERAGE, COUNT, MAX, or MIN
See if this works
VBA Code:
FltCriteria = FltCriteria & " AND [PhContactSub]LIKE " & "'" & "*" & FilterNo2Check & "*" & "'"
 
Upvote 0
Not wrong, but you can save yourself some typing in the future by not concatenating every wildcard or single quote character, plus less chance of messing up maybe:
LIKE " & "'" & "*" & FilterNo1Check & "*" & "'"
vs
LIKE '*" & FilterNo2Check & "*'"
 
Upvote 0

Forum statistics

Threads
1,224,590
Messages
6,179,756
Members
452,940
Latest member
rootytrip

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