Multiple Filter for a Column

rkunal

New Member
Joined
Aug 22, 2019
Messages
2
Hi,

I am trying to add multiple filter in a column using macros.

For Example: In a column, I want to filter cells containing apple and orange but not banana or guava. So there are 4 conditions.

For 2 conditions, I am able to create below code but don't know how to extend this to satisfy all conditions.

HTML:
Sub Macro4()'' Macro4 Macro'
'    ActiveSheet.Range("$A:$A").AutoFilter Field:=1, Criteria1:="=*apple*" _        , Operator:=xlAnd, Criteria2:="<>*banana*"End Sub

I am new to macros so please help me with this. Thanks in advance.

--
Kunal
 

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.
Hi,

I am trying to add multiple filter in a column using macros.

For Example: In a column, I want to filter cells containing apple and orange but not banana or guava. So there are 4 conditions.

For 2 conditions, I am able to create below code but don't know how to extend this to satisfy all conditions.

HTML:
Sub Macro4()'' Macro4 Macro'
'    ActiveSheet.Range("$A:$A").AutoFilter Field:=1, Criteria1:="=*apple*" _        , Operator:=xlAnd, Criteria2:="<>*banana*"End Sub

I am new to macros so please help me with this. Thanks in advance.

--
Kunal

You say theres 4 condition but it looks like there's only 2.

You want to see Only apple orange so change Criteria2 to ="=*Orange*"

This will show only those 2. If you are filtering a pivot table it is a little different.
 
Upvote 0
Thanks for replying. I have added an example for better understanding of my criteria.

*My criteria: The cell should contain either apple OR orange but should not contain either banana OR guava.

[TABLE="class: grid, width: 336"]
<tbody>[TR]
[TD]Values[/TD]
[TD]Result after filter[/TD]
[/TR]
[TR]
[TD]Apple[/TD]
[TD]Apple[/TD]
[/TR]
[TR]
[TD]Apple Orange[/TD]
[TD]Apple Orange[/TD]
[/TR]
[TR]
[TD]Apple Banana[/TD]
[TD]Orange[/TD]
[/TR]
[TR]
[TD]Apple Guava[/TD]
[TD]Orange Apple[/TD]
[/TR]
[TR]
[TD]Orange[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]Orange Apple[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]Orange Banana[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]Orange Guava[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]Banana[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]Banana Apple[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]Banana Orange[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]Banana Guava[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]Guava[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]Guava Apple[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]Guava Orange[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]Guava Banana[/TD]
[TD][/TD]
[/TR]
</tbody>[/TABLE]
 
Upvote 0
Thanks for replying. I have added an example for better understanding of my criteria.

*My criteria: The cell should contain either apple OR orange but should not contain either banana OR guava.

[TABLE="class: grid, width: 336"]
<tbody>[TR]
[TD]Values[/TD]
[TD]Result after filter[/TD]
[/TR]
[TR]
[TD]Apple[/TD]
[TD]Apple[/TD]
[/TR]
[TR]
[TD]Apple Orange[/TD]
[TD]Apple Orange[/TD]
[/TR]
[TR]
[TD]Apple Banana[/TD]
[TD]Orange[/TD]
[/TR]
[TR]
[TD]Apple Guava[/TD]
[TD]Orange Apple[/TD]
[/TR]
[TR]
[TD]Orange[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]Orange Apple[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]Orange Banana[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]Orange Guava[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]Banana[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]Banana Apple[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]Banana Orange[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]Banana Guava[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]Guava[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]Guava Apple[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]Guava Orange[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]Guava Banana[/TD]
[TD][/TD]
[/TR]
</tbody>[/TABLE]

Can you filter by Apple, Apple Orange, Orange, and Orange Apple? Or is your data more complex then that?
 
Upvote 0
Try this

Code:
Sub Multiple_Filter()
   Dim c As Range, Rng As Range, i As Long, j As Long
   Dim inis() As Variant, outs() As Variant, exists As Boolean
   inis = Array("apple", "orange", "lemon") [B][COLOR=#008000]'should contain[/COLOR][/B]
   outs = Array("banana", "guava", "melon") [B][COLOR=#ff0000]'but should not contain[/COLOR][/B]
   
   With ActiveSheet
      Set Rng = .Range("A2", .Range("A" & Rows.Count).End(xlUp))
      ReDim ary(Rng.Count)
      For Each c In Rng
        exists = False
        For j = 0 To UBound(inis)
          If InStr(1, LCase(c), LCase(inis(j))) > 0 Then
            exists = True
            Exit For
          End If
        Next
        For j = 0 To UBound(outs)
          If InStr(1, LCase(c), LCase(outs(j))) > 0 Then
            exists = False
            Exit For
          End If
        Next
        If exists Then
          ary(i) = c.Value
          i = i + 1
        End If
      Next c
      .Range("A:A").AutoFilter 1, ary, xlFilterValues
   End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,909
Messages
6,175,314
Members
452,634
Latest member
cpostell

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