Using 'not equal to' an array as criteria in AutoFilter in Excel VBA

sharshra

Active Member
Joined
Mar 20, 2013
Messages
404
Office Version
  1. 365
In my the spread sheet, I have defined a range, which needs to be excluded from the filter. The values to exclude are filled dynamically filled in the range & will be more than 3 in most of the cases. I intend to use the code similar to what is shown below. varFilterValues is an array populated with values from the named range. But I'm struggling on how to exclude these from the filter. I'm sure it must be a silly mistake in defining the 'not equal to' array in Criteria1. Can any one point where the mistake is please? I searched internet but couldn't find simple & appropriate solution.
Code:
[B]    ActiveSheet.Range("$A$1:$V$100").AutoFilter Field:=9, _
        Criteria1:[COLOR=#ff0000]<>varFilterValues[/COLOR], _
        Operator:=xlAnd[/B]

I know the following code works when I want to add filter using similar method & define criteria 'equal' to an array. I was expecting similar for 'not equal to' an array condition also.
Code:
    ActiveSheet.Range("$A$1:$V$100").AutoFilter Field:=9, _
        Criteria1:[COLOR=#008000]=varFilterValues[/COLOR], _
        Operator:=xlFilterValues
 

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.
Hello Sharshra,

Try one of the following methods:-

Code:
[B][COLOR=#ff0000]"<>varFilterValues"[/COLOR][/B]

or

Code:
[B][COLOR=#ff0000]"<>" & varFilterValues[/COLOR][/B]

Whichever way, the <> needs to be enclosed with quotation marks.

I hope that this helps.

Cheerio,
vcoolio.
 
Upvote 0
Thanks, vcoolio for quick response :)

Code:
[COLOR=#ff0000]"<>" & varFilterValues[/COLOR]
throws an error 'type mismatch'.

Code:
[COLOR=#ff0000]"<>varFilterValues"[/COLOR]
is not throwing any error, but it is not actually filtering. It is as good as no filter applied!:confused: I think it reads "<>varFilterValues" as just a string & try to filter "<>varFilterValues", which obviously is not there, resulting nothing being filtered.

Is there any other ways to achieve this?
 
Upvote 0
jindon posted this yesterday.
Code:
Sub test()
    Dim e
    Application.ScreenUpdating = False
    With Cells(1).CurrentRegion
        For Each e In Array(Array("coupon", 3), Array("blu", 4))
            .AutoFilter 1, "<>*" & e(0) & "*"
            .Columns(e(1)).Offset(1).ClearContents
            .AutoFilter
        Next
    End With
    Application.ScreenUpdating = True
End Sub
You might be able to change it to fit your needs.
 
Upvote 0
@jolivanes
Thanks for the link.
@sharshra
It may be possible to modify jindon's approach, depending on what you are try to do.
What are you trying to do with the filtered data?
 
Upvote 0
I modified the code posted by jindon & tried the following. Still getting the error 'Type mismatch'. It erring at &. :(:confused:
Code:
Criteria1:="<>*" [COLOR=#ff0000][B]&[/B][/COLOR] varFilterValues & "*"

Hi Fluff,
I have a named range to list the values to remove from the filter. The values in this named range is populated based on different criteria. varFilterValues is an array & contains these values from the named range. When I filter a column, I have to exclude these values stored in varFilterValues.
 
Upvote 0
In that case you will not be able to use autofilter, as you are limited to 2 values.
Have a look at advanced filters
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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