multiple filter selection using VBA macro

mobeenraheem

New Member
Joined
Oct 24, 2014
Messages
4
Hi

I am trying to achieve multiple filter selection using VBA macro but its not working as it should.

Below macro is working fine when there is two values.its not working more then two values.
Can please someone tell me what i am missing.

Code:
Sub Macro1()
'
' Macro1 Macro
'


'
    Columns("A:A").Select
    Selection.AutoFilter
    Range("A1").Select
    ActiveSheet.Range("$A$1:$A$11076").AutoFilter Field:=1, Criteria1:=Array( _
        "=*value1)*", "=*value2*", "=*value3*", "=*value4*"), Operator:=xlFilterValues
End Sub
 

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
suppose data is like this

hdng1hdng2
aa1
as2
dd3
fg4
gh5
as6
sd7
df8
fg9
gh0
a9
hg8
gf7
fg6
jg5

<colgroup><col width="64" span="2" style="width:48pt"> </colgroup><tbody>
</tbody>




somehow more tha two criteria does not work even using array function. experts may give better explanation. I subvert this by this macro. try that and use it by modifying.see variable x(1),x(2) and x(3)
the result of macro is in sheet2

Code:
Sub test()
Dim x(1 To 3), j As Integer
Dim r As Range
x(1) = "a*"
x(2) = "s*"
x(3) = "d*"
Worksheets("sheet2").Cells.Clear
Worksheets("sheet1").Activate
Set r = Range("A1").CurrentRegion


r.AutoFilter field:=1, Criteria1:=Array(x(1), x(2)), Operator:=xlFilterValues
r.Offset(1, 0).Resize(r.Rows.Count - 1).Copy Worksheets("sheet2").Cells(Rows.Count, "A").End(xlUp).Offset(1, 0)
    
     ActiveSheet.AutoFilterMode = False
     r.AutoFilter field:=1, Criteria1:=x(3)
    r.Offset(1, 0).Resize(r.Rows.Count - 1).Copy Worksheets("sheet2").Cells(Rows.Count, "A").End(xlUp).Offset(1, 0)
     ActiveSheet.AutoFilterMode = False
MsgBox "macro done see sheete2"
End Sub
 
Upvote 0
Below macro is working fine when there is two values.its not working more then two values.
Can please someone tell me what i am missing.
Using AutoFilter with Text Filters as you are trying to do here only allows two values - It is just an Excel limit.
If you manually try AutoFilter in the sheet and choose 'Text Filters' from the drop-down, you only get the choice of two, whether it be "Equals", "Begins with", "Contains" etc.
 
Upvote 0

Forum statistics

Threads
1,214,646
Messages
6,120,716
Members
448,985
Latest member
chocbudda

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