Auto Filter all except......

SueKi

New Member
Joined
Feb 15, 2022
Messages
44
Office Version
  1. 365
Platform
  1. Windows
I need to get rid of rows of data where column D is not equal to SOM, MHT or ELP. My first pass was to filter for certain items in a different column, then select the range and delete those rows. That worked fine, but then more items got added and I hadn't filtered them. So, I need to be able to say filter everything except on a different column.

The below worked for my filter until I changed the criteria to a selection of items to exclude(<>). Do I have a syntax problem, or can I not do multiple exclusions like this?

VBA Code:
Dim FilterRange As Long
FilterRange = Range("a" & Rows.Count).End(xlUp).Row
       
    Range("A1").Select
    ActiveCell.Offset(2, 0).Range("A1").Select
    Selection.AutoFilter
    ActiveSheet.Range("$A$3:$L$" & FilterRange).AutoFilter Field:=4, Criteria1:=Array("<>MHT" _
        , "<>SOM", "<>ELP"), Operator:=xlFilterValues
 

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
I need to get rid of rows of data where column D is not equal to SOM, MHT or ELP. My first pass was to filter for certain items in a different column, then select the range and delete those rows. That worked fine, but then more items got added and I hadn't filtered them. So, I need to be able to say filter everything except on a different column.

The below worked for my filter until I changed the criteria to a selection of items to exclude(<>). Do I have a syntax problem, or can I not do multiple exclusions like this?

VBA Code:
Dim FilterRange As Long
FilterRange = Range("a" & Rows.Count).End(xlUp).Row
      
    Range("A1").Select
    ActiveCell.Offset(2, 0).Range("A1").Select
    Selection.AutoFilter
    ActiveSheet.Range("$A$3:$L$" & FilterRange).AutoFilter Field:=4, Criteria1:=Array("<>MHT" _
        , "<>SOM", "<>ELP"), Operator:=xlFilterValues
I believe you can only do a maximum of 2 notequal to criteria in a filter. Advanced filters may be the way forward
 
Upvote 0
I believe you can only do a maximum of 2 notequal to criteria in a filter. Advanced filters may be the way forward
Ugh.... you seem to be right. I took it to 2, it failed and down to just one and it worked.

At least I'll stop fighting with it!

Thanks!
 
Upvote 0
I need to get rid of rows of data where column D is not equal to SOM, MHT or ELP. My first pass was to filter for certain items in a different column, then select the range and delete those rows. That worked fine, but then more items got added and I hadn't filtered them. So, I need to be able to say filter everything except on a different column.

The below worked for my filter until I changed the criteria to a selection of items to exclude(<>). Do I have a syntax problem, or can I not do multiple exclusions like this?

VBA Code:
Dim FilterRange As Long
FilterRange = Range("a" & Rows.Count).End(xlUp).Row
      
    Range("A1").Select
    ActiveCell.Offset(2, 0).Range("A1").Select
    Selection.AutoFilter
    ActiveSheet.Range("$A$3:$L$" & FilterRange).AutoFilter Field:=4, Criteria1:=Array("<>MHT" _
        , "<>SOM", "<>ELP"), Operator:=xlFilterValues
As @gordsky pointed out, you can't use more than 2 <> in an AutoFilter, however, there is a workaround you can use without going to an advanced filter. Try the following on a copy of your data (assumes your data has headings in row 2).

VBA Code:
Option Explicit
Sub Exclude_3()
    Dim d As Object, i As Long, c As Range, tmp As String, x
    
    Set d = CreateObject("scripting.dictionary")
    x = Application.Transpose(Range("D3", Cells(Rows.Count, "D").End(xlUp)))
    
    For i = 1 To UBound(x, 1)
    d(x(i)) = 1
    Next
    
    For Each c In Range("D3", Cells(Rows.Count, "D").End(xlUp))
        tmp = c.Value
        If d.exists(tmp) And (tmp) Like "SOM" Then d.Remove (tmp) '<< removes "SOM"
        If d.exists(tmp) And (tmp) Like "MHT" Then d.Remove (tmp) '<< removes "MHT"
        If d.exists(tmp) And (tmp) Like "ELP" Then d.Remove (tmp) '<< removes "ELP"
    Next
    
    With Range("A3").CurrentRegion
        .AutoFilter 4, Array(d.keys), 7 '<< Filters on the remaining values
        .Offset(1).EntireRow.Delete
        .AutoFilter
    End With
End Sub
 
Upvote 0
Solution
As @gordsky pointed out, you can't use more than 2 <> in an AutoFilter, however, there is a workaround you can use without going to an advanced filter. Try the following on a copy of your data (assumes your data has headings in row 2).

VBA Code:
Option Explicit
Sub Exclude_3()
    Dim d As Object, i As Long, c As Range, tmp As String, x
  
    Set d = CreateObject("scripting.dictionary")
    x = Application.Transpose(Range("D3", Cells(Rows.Count, "D").End(xlUp)))
  
    For i = 1 To UBound(x, 1)
    d(x(i)) = 1
    Next
  
    For Each c In Range("D3", Cells(Rows.Count, "D").End(xlUp))
        tmp = c.Value
        If d.exists(tmp) And (tmp) Like "SOM" Then d.Remove (tmp) '<< removes "SOM"
        If d.exists(tmp) And (tmp) Like "MHT" Then d.Remove (tmp) '<< removes "MHT"
        If d.exists(tmp) And (tmp) Like "ELP" Then d.Remove (tmp) '<< removes "ELP"
    Next
  
    With Range("A3").CurrentRegion
        .AutoFilter 4, Array(d.keys), 7 '<< Filters on the remaining values
        .Offset(1).EntireRow.Delete
        .AutoFilter
    End With
End Sub
Wow! Thank you! Its going to take me some time to learn to understand this one!
 
Upvote 0

Forum statistics

Threads
1,214,421
Messages
6,119,392
Members
448,891
Latest member
tpierce

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