1

How to filter OUT an array through VBA. I mean, I want to exclude arr array from filed 29. arr is a long list. The following snippet does not work.

arr = Array("<>0735", "<>801124", "<>0613","<>0921", "<>1086", "<>0949", "<>0494", "<>0767", "<>0739")
MySheet.Range("AB1").AutoFilter _
    Field:=29, _
    Criteria1:=arr, _
    Operator:=xlFilterValues
3
  • Try to record a macro, excluding the 29 manually and see the recorded code. Commented Jan 3, 2021 at 11:22
  • What does does not work mean? Which column are you trying to filter by (e.g. AC, BD or ...)? Posting an image of the data would surely benefit all readers. Commented Jan 3, 2021 at 12:34
  • Excel/VBA do not allow to use more the two such excluding criteria. You can do it only using an extra column where to use formulas able to return the same String/Boolean for all involved conditions and then filter according to it. Commented Jan 3, 2021 at 14:51

3 Answers 3

3

If you want to use AutoFilter and exclude several values, create an array that includes only the "good" values. Say we start with:

enter image description here

and we want to exclude the Stooges. We need to create a "Stooge-free" array":

Sub NoStooges()
    Dim rng As Range, c As Collection
    Dim r As Range, v As String, n As Long
    Dim i As Long, arr
    
    Set rng = Range("A2:A20")
    Set c = New Collection
    
    For Each r In rng
        v = r.Value
        If v <> "Larry" And v <> "Moe" And v <> "Curley" Then
            On Error Resume Next
                c.Add v, CStr(v)
            On Error GoTo 0
        End If
    Next r
    
    n = c.Count
    
    ReDim arr(1 To n)
    For i = 1 To n
        arr(i) = c.Item(i)
    Next i
    
    With ActiveSheet
        If .FilterMode Then Cells.AutoFilter
        .Range("$A$1:$A$20").AutoFilter
        .Range("$A$1:$A$20").AutoFilter Field:=1, Criteria1:=(arr), Operator:=xlFilterValues
    End With
        
    
End Sub

The array arr and its associated Collection have only three elements {a, b, c} . The code produces:

enter image description here

Sign up to request clarification or add additional context in comments.

2 Comments

Friendly hint: A worksheet's .FilterMode property is read-only due to [MS Help](learn.microsoft.com/de-de/office/vba/api/… )
@T.M. Thanks...................I fixed the code...................
1

Using the FILTER function might get you the functionality you are looking for.

=FILTER(<data>, ISNA(MATCH(<criteria column>, <exclude list>, 0)))

This will filter out all of the rows in where the value in matches something in the .

1 Comment

... or including the three-names array instead of a column range =FILTER(A2:A20,ISNA(MATCH(A2:A20,{"Curley","Moe","Larry"},0))). Anyway a valid answer for current dynamic versions (MS 365/2019). Would have been nice to add/edit some explanations why this works (at least for newbies :-)
0

Here's an existing SO answer that may help. Based on this response, it sounds like you may need a workaround since vba can't handle multiple criteria like this in a single column:

https://stackoverflow.com/a/28579593/1277402

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.