Autofilter with array

pavanai

New Member
Joined
Jun 10, 2020
Messages
3
Office Version
  1. 2019
  2. 2010
Platform
  1. Windows
I am trying to do an autofilter with an array and encountering " Error: AutoFilter method of Range Class failed "

Here is the vba code:

Dim sArray() as Variant

sArray = Array("MS*", "<>*ML*")
ActiveSheet.Range("A1:Q" & LastRow).AutoFilter Field:=3, Criteria1:=sArray, Operator:=xlFilterValues

Code works if i replace <>*ML" with any other value.. Any idea whats wrong?
 

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
What happens with
VBA Code:
ActiveSheet.Range("A1:Q" & LastRow).AutoFilter Field:=3, Criteria1:="<>*ML*"
You don't need an array for the example provided, anything equal to MS* will be <>*ML* so the first part is redundant.
 
Upvote 0
Hi & welcome to MrExcel.
How about
VBA Code:
 ActiveSheet.Range("A1:Q" & LastRow).AutoFilter Field:=3, Criteria1:="MS*", Operator:=xlOr, Criteria2:="<>*ML*"
 
Upvote 0
Thanks for the response. I added MS* for debugging. I basically wanted to add 2 "does not contain" conditions in my array and then use that array in the filter. Vba is not letting me do that.
 
Upvote 0
Thanks for the response. I added MS* for debugging. I basically wanted to add 2 "does not contain" conditions in my array and then use that array in the filter. Vba is not letting me do that.
The array in your OP does not reflect 2 "does not contain" conditions. What are the 2 conditions you want to use?
 
Upvote 0
You should really only use an array for exact matches, if you only have two criteria, do like I showed in post#3
 
Upvote 0
As far as I'm aware, you can't use 'does not contain' as part of an array, you can only use the method that Fluff has shown you. An array is only suitable for a list of 'equal to' items (which can use wildcards).
 
Upvote 0
Do you want does not Contain, or does not start with?
 
Upvote 0
You can use an array for 2 "does not contain" conditions, but it's not necessary.

This will work:
VBA Code:
Dim sArray() As Variant
sArray = Array("<>data10", "<>*ML*")
ActiveSheet.Range("A1:Q" & LastRow).AutoFilter Field:=3, Criteria1:=sArray(0), Operator:=xlAnd, Criteria2:=sArray(1)
 
Upvote 0

Forum statistics

Threads
1,214,938
Messages
6,122,346
Members
449,080
Latest member
Armadillos

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