Problem with multiple criteria in VBA

Klash Ville

Board Regular
Joined
Sep 19, 2017
Messages
83
I have the following code:

Code:
.Range.AutoFilter Field:=.ListColumns("Type").Index, Criteria1:="=Phone", Operator:=xlAnd, Criteria2:="=Router", Operator:=xlAnd, Criteria3:="=Telecommunications Equipment", Operator:=xlAnd, Criteria4:="=Network Product", Operator:=xlAnd, Criteria5:="=Headset"

When running this code, the following error message returns:


Run time error '91':

Object Variable or With Block variable not set
 

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.
Please post the rest of your code.
That error message usually indicates that you have not properly closed out of a WITH, IF THEN, or LOOP statement.
We would need to see the rest of the code to see what might be missing.
 
Upvote 0
Please post the rest of your code.
That error message usually indicates that you have not properly closed out of a WITH, IF THEN, or LOOP statement.
We would need to see the rest of the code to see what might be missing.

Code:
With ActiveSheet.ListObjects("Table2")
        .Range.AutoFilter Field:=.ListColumns("UK Category").Index
        .Range.AutoFilter Field:=.ListColumns("Description").Index
        .Range.AutoFilter Field:=.ListColumns("Type").Index, Criteria1:="=Phone", Operator:=xlAnd, Criteria2:="=Router", Operator:=xlAnd, Criteria3:="=Telecommunications Equipment", Operator:=xlAnd, Criteria4:="=Network Product", Operator:=xlAnd, Criteria5:="=Headset"
End With

It's a very simple With and End With, I have used so many times before, but, it's the first time I am filtering for 5 values, so... I guess it's because of the size?
 
Last edited:
Upvote 0
I decided to only use the filtering of the 5 values, without the 2 filtering above, the following error returns:

Run time error '1004':

Application-defined or object-defined error
 
Upvote 0
Does it work with just 2 filters?
If you still get the same error message, then I don't suspect the error is actually with this part of the code, but with you "bigger" code.
I am not 100% certain, but I think you can filter using at least 3 criteria. I am not certain if you can do more than that. But you should be able to test that pretty easily to see if that is the case (see if 3 works, then see if 4 works).
 
Upvote 0
Does it work with just 2 filters?
If you still get the same error message, then I don't suspect the error is actually with this part of the code, but with you "bigger" code.
I am not 100% certain, but I think you can filter using at least 3 criteria. I am not certain if you can do more than that. But you should be able to test that pretty easily to see if that is the case (see if 3 works, then see if 4 works).


Yap, it works with only 1 filter, and 2 filters maximum. With 3, 4, 5 etc... it throws error... I guess it's because the xlAnd only works with 2? =/
 
Upvote 0
You may need to use Advanced Filters to do more than 2.
 
Upvote 0
I never used such thing before, where can I find how to use that?
Do a Google Search, and there are lots of tutorials available on it.
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,327
Members
452,635
Latest member
laura12345

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