0

I've been trying to use VBA's advanced filter method to exclude a set of criteria. Here is a simplified example.

enter image description here

My criteria are stored in the worksheet "Criteria", as such.

enter image description here

I now want to filter the data, such that any entry containing the name "Joe" OR the job "Pilot" are excluded. I have tried the following:

Sub filter_data()
 With Worksheets("Data")
  .Activate
  .Range("A1:C10").AdvancedFilter _
  Action:= xlFilterInPlace, _
  CriteriaRange:= Worksheets("Criteria").Range("A1:C3")
 End With
End Sub

I checked if my code filters the data if I don't use the <> operator. This worked like a charm and included only entries containing "Joe" OR "Pilot". However, excluding the said entries does not work. Can anybody help?

2
  • Your filter will INCLUDE all entries where Name is not Joe OR Job is not pilot. Is that what you want? If not, you need to express your filter criteria in terms of which rows you want to INCLUDE. Commented May 27, 2021 at 18:21
  • Yes, that is what I need. I want to include every entry that does not contain "Joe" OR "Pilot". Ultimately, I should be left with 5 entries and none of them contain the name "Joe" OR the job "Pilot". However, running my code like this filters nothing. Commented May 27, 2021 at 18:33

1 Answer 1

2

Since you are looking for the negative you want AND not Or.

Then in A2 put: <>Joe

Then in C2 put: <>Pilot

So it looks like this:

enter image description here

Then change the reference to only the two rows:

Sub filter_data()

    With Worksheets("Data")
        .Range("A1:C10").AdvancedFilter _
        Action:=xlFilterInPlace, _
        CriteriaRange:=Worksheets("Criteria").Range("A1:C2")
    End With
End Sub

enter image description here

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

1 Comment

Thank you, that is what I was looking for. In my real data set, however; I also have cases where I want to exclude entries based on criteria 1 AND criteria 2. In this example, how do I exclude only entries where the name is "Joe" AND the job is "Doctor". Hence, only the entry in row 3 should be excluded. Is there also a clear differentiation between AND/ OR in the case where I use <>?

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.