filter with multiple criteria and partial values

Tanyaann1995

Board Regular
Joined
Mar 24, 2021
Messages
62
Office Version
  1. 2016
Platform
  1. Windows
Hi,

Currently, I am using the below code for filtering criteria with partial value only:

VBA Code:
With Workbooks("Latest hist prices 4.xlsm").Worksheets(2)
        .Range("A1:U79578").AutoFilter Field:=4, Criteria1:="=*" & cname & "*"

Is there a way to filter with multiple criteria but the criteria is just a partial value just like the above? I tried to edit the above code as given below but the data is not getting filtered with the partial value as criteria. The sheet shows blank cells because nothing is getting filtered.

VBA Code:
With Workbooks("Latest hist prices 4.xlsm").Worksheets(2)
        .Range("A1:U79578").AutoFilter Field:=4, Criteria1:="=*" & cname1 & "*", Operator:=xlAND, Criteria2:="=*" & cname2 & "*"
 

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
You need to use xlOr rather than xlAnd
 
Upvote 0
Solution
Hi,

Currently, I am using the below code for filtering criteria with partial value only:

VBA Code:
With Workbooks("Latest hist prices 4.xlsm").Worksheets(2)
        .Range("A1:U79578").AutoFilter Field:=4, Criteria1:="=*" & cname & "*"

Is there a way to filter with multiple criteria but the criteria is just a partial value just like the above? I tried to edit the above code as given below but the data is not getting filtered with the partial value as criteria. The sheet shows blank cells because nothing is getting filtered.

VBA Code:
With Workbooks("Latest hist prices 4.xlsm").Worksheets(2)
        .Range("A1:U79578").AutoFilter Field:=4, Criteria1:="=*" & cname1 & "*", Operator:=xlAND, Criteria2:="=*" & cname2 & "*"
Hi,

Thanks, the above worked.
I have a form that asks the user to fill in the customer name that they want to filter against. The user can add either 1 or more than 1 customer. If more than 1 customer is added, the below code works perfectly. But, if there is only one customer name added, then the filtered results include all the customers in the list. Please advise if there is a way to use the below code to filter with just 1 customer if the user adds only 1 customer.

VBA Code:
With UserForm1
 .Show
 cname1 = .TextBox1.Value
 cname2 = .TextBox3.Value
 ename = .TextBox2.Value
 End With

With Workbooks("Latest hist prices 4.xlsm").Worksheets(3)
.Range("A1:U79578").AutoFilter Field:=4, Criteria1:="=*" & cname1 & "*", Operator:=xlOr, Criteria2:="=*" & cname2 & "*"
        .Range("A1:U79578").AutoFilter Field:=6, Criteria1:="=*" & ename & "*"
 
Upvote 0
How about
VBA Code:
With Workbooks("Latest hist prices 4.xlsm").Worksheets(3)
   If cname2 = "" Then
      .Range("A1:U79578").AutoFilter Field:=4, Criteria1:="=*" & cname1 & "*"
              .Range("A1:U79578").AutoFilter Field:=6, Criteria1:="=*" & ename & "*"
   Else
      .Range("A1:U79578").AutoFilter Field:=4, Criteria1:="=*" & cname1 & "*", Operator:=xlOr, Criteria2:="=*" & cname2 & "*"
              .Range("A1:U79578").AutoFilter Field:=6, Criteria1:="=*" & ename & "*"
   End If
End With
 
Upvote 0
How about
VBA Code:
With Workbooks("Latest hist prices 4.xlsm").Worksheets(3)
   If cname2 = "" Then
      .Range("A1:U79578").AutoFilter Field:=4, Criteria1:="=*" & cname1 & "*"
              .Range("A1:U79578").AutoFilter Field:=6, Criteria1:="=*" & ename & "*"
   Else
      .Range("A1:U79578").AutoFilter Field:=4, Criteria1:="=*" & cname1 & "*", Operator:=xlOr, Criteria2:="=*" & cname2 & "*"
              .Range("A1:U79578").AutoFilter Field:=6, Criteria1:="=*" & ename & "*"
   End If
End With
Thanks, this worked :)
 
Upvote 0
You're welcome & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,215,049
Messages
6,122,864
Members
449,097
Latest member
dbomb1414

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