Not equal to in vba array

honkin

Active Member
Joined
Mar 20, 2012
Messages
385
Office Version
  1. 2016
Platform
  1. MacOS
In a VBA array, how does one exclude values?

I am trying to autofilter for everything that is not equal to 1, 5 or blank in column 10, but the line of code is obviously incorrect.

.AutoFilter Field:=10, Criteria1:=Array("<>1", "<>5", "<>"), Operator:=xlFilterValues

cheers
 

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
If this is a sheet array formula you're creating in vba, I don't know how. If it's a vba array as your title suggests, you don't actually see the values, so I think that is not a vba array you're using. However, in case that is the intent, you add items to an array then refer to them by their index number (0 based index). You would have to test an array element and act accordingly
IF aryMyArray(2) <> "5" Then

or loop over the elements using a counter variable.

Please completely disregard if you're dealing with an array in a formula, even if it's being created with vba.
 
Upvote 0
If this is a sheet array formula you're creating in vba, I don't know how. If it's a vba array as your title suggests, you don't actually see the values, so I think that is not a vba array you're using. However, in case that is the intent, you add items to an array then refer to them by their index number (0 based index). You would have to test an array element and act accordingly
IF aryMyArray(2) <> "5" Then

or loop over the elements using a counter variable.

Please completely disregard if you're dealing with an array in a formula, even if it's being created with vba.
cheers Micron

Yes, that line is part of about 5 other autofilter criteria; all the rest are filtering for = and many are single value, with a couple being arrays. It's always easy to filter for when things are equal to, but on this line, there are so many possible numbers in column J (10) so thought it was easier to just try and exclude the 3 not required.

Ta
 
Upvote 0
As far as I know, you can't exclude more than 2 criteria using AutoFilter. There are workarounds however. In the method below, you use a dictionary to create a unique set of all values in column 10, then go through the keys - removing any that are 1 or 5. Finally you use the remaining keys as your array to filter on column 10 (blanks are excluded by default).

VBA Code:
Option Explicit
Sub Exclude_3()
    Dim d As Object, c As Range, tmp As String, x, i As Long, ws As Worksheet
    Set ws = ActiveSheet
    Set d = CreateObject("scripting.dictionary")
    x = Application.Transpose(Range("J2", Cells(Rows.Count, "J").End(xlUp)))
    
    For i = 1 To UBound(x, 1)
        d(x(i)) = 1
    Next

    For Each c In ws.Range("J2", Cells(Rows.Count, "J").End(xlUp)) '<< change Column/Range to suit
        tmp = c.Value
        If d.exists(tmp) And (tmp) = "1" Then d.Remove (tmp) '<< removes 1
        If d.exists(tmp) And (tmp) = "5" Then d.Remove (tmp) '<< removes 5
    Next
    
    With ws.Cells(1, 1).CurrentRegion
        .AutoFilter 10, Array(d.keys), 7
    End With
End Sub

Before:

filter 3.xlsb
ABCDEFGHIJ
1header1header2header3header4header5header6header7header8header9header10
2datadatadatadatadatadatadatadatadata1
3datadatadatadatadatadatadatadatadata5
4datadatadatadatadatadatadatadatadata
5datadatadatadatadatadatadatadatadataa
6datadatadatadatadatadatadatadatadatab
7datadatadatadatadatadatadatadatadatac
8datadatadatadatadatadatadatadatadata1
9datadatadatadatadatadatadatadatadata5
10datadatadatadatadatadatadatadatadata
11datadatadatadatadatadatadatadatadatax
12datadatadatadatadatadatadatadatadatay
13datadatadatadatadatadatadatadatadataz
14
Sheet1


After:

filter 3.xlsb
ABCDEFGHIJ
1header1header2header3header4header5header6header7header8header9header10
5datadatadatadatadatadatadatadatadataa
6datadatadatadatadatadatadatadatadatab
7datadatadatadatadatadatadatadatadatac
11datadatadatadatadatadatadatadatadatax
12datadatadatadatadatadatadatadatadatay
13datadatadatadatadatadatadatadatadataz
14
Sheet1
 
Upvote 0
As far as I know, you can't exclude more than 2 criteria using AutoFilter. There are workarounds however. In the method below, you use a dictionary to create a unique set of all values in column 10, then go through the keys - removing any that are 1 or 5. Finally you use the remaining keys as your array to filter on column 10 (blanks are excluded by default).

VBA Code:
Option Explicit
Sub Exclude_3()
    Dim d As Object, c As Range, tmp As String, x, i As Long, ws As Worksheet
    Set ws = ActiveSheet
    Set d = CreateObject("scripting.dictionary")
    x = Application.Transpose(Range("J2", Cells(Rows.Count, "J").End(xlUp)))
   
    For i = 1 To UBound(x, 1)
        d(x(i)) = 1
    Next

    For Each c In ws.Range("J2", Cells(Rows.Count, "J").End(xlUp)) '<< change Column/Range to suit
        tmp = c.Value
        If d.exists(tmp) And (tmp) = "1" Then d.Remove (tmp) '<< removes 1
        If d.exists(tmp) And (tmp) = "5" Then d.Remove (tmp) '<< removes 5
    Next
   
    With ws.Cells(1, 1).CurrentRegion
        .AutoFilter 10, Array(d.keys), 7
    End With
End Sub

Before:

filter 3.xlsb
ABCDEFGHIJ
1header1header2header3header4header5header6header7header8header9header10
2datadatadatadatadatadatadatadatadata1
3datadatadatadatadatadatadatadatadata5
4datadatadatadatadatadatadatadatadata
5datadatadatadatadatadatadatadatadataa
6datadatadatadatadatadatadatadatadatab
7datadatadatadatadatadatadatadatadatac
8datadatadatadatadatadatadatadatadata1
9datadatadatadatadatadatadatadatadata5
10datadatadatadatadatadatadatadatadata
11datadatadatadatadatadatadatadatadatax
12datadatadatadatadatadatadatadatadatay
13datadatadatadatadatadatadatadatadataz
14
Sheet1


After:

filter 3.xlsb
ABCDEFGHIJ
1header1header2header3header4header5header6header7header8header9header10
5datadatadatadatadatadatadatadatadataa
6datadatadatadatadatadatadatadatadatab
7datadatadatadatadatadatadatadatadatac
11datadatadatadatadatadatadatadatadatax
12datadatadatadatadatadatadatadatadatay
13datadatadatadatadatadatadatadatadataz
14
Sheet1
Cheers Kevin
A quick question; does this actually remove the rows with 1, 5 & blank, or simply hide them?
 
Upvote 0
It just hides them. The AutoFilter is actually on, you just can't see the down arrows with the XL2bb mini-sheet. You can remove them as well if you want.
 
Upvote 0
It just hides them. The AutoFilter is actually on, you just can't see the down arrows with the XL2bb mini-sheet. You can remove them as well if you want.
Then it seems a perfect workaround mate. Cheers. I'll copy and paste it and see how it goes. Ta!
 
Upvote 0

Forum statistics

Threads
1,224,271
Messages
6,177,601
Members
452,784
Latest member
talippo

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