Filter column based on another column

m_sabeer

New Member
Joined
Sep 12, 2013
Messages
24
Hi,
I wish to filter a column based on an input range in same (or another) column for eg.


[TABLE="class: grid, width: 201"]
<tbody>[TR]
[TD][/TD]
[TD="align: center"]A[/TD]
[TD="align: center"]B[/TD]
[/TR]
[TR]
[TD="align: center"]1[/TD]
[TD="align: center"]Account[/TD]
[TD="align: center"]Value[/TD]
[/TR]
[TR]
[TD="align: center"]2[/TD]
[TD="align: center"]bbb[/TD]
[TD="align: center"]150[/TD]
[/TR]
[TR]
[TD="align: center"]3[/TD]
[TD="align: center"]aaa[/TD]
[TD="align: center"]100[/TD]
[/TR]
[TR]
[TD="align: center"]4[/TD]
[TD="align: center"]ccc[/TD]
[TD="align: center"]200[/TD]
[/TR]
[TR]
[TD="align: center"]5[/TD]
[TD="align: center"]aaa[/TD]
[TD="align: center"]150[/TD]
[/TR]
[TR]
[TD="align: center"]6[/TD]
[TD="align: center"]bbb[/TD]
[TD="align: center"]250[/TD]
[/TR]
[TR]
[TD="align: center"]7[/TD]
[TD="align: center"]vvv[/TD]
[TD="align: center"]200[/TD]
[/TR]
[TR]
[TD="align: center"]8[/TD]
[TD="align: center"]aaa[/TD]
[TD="align: center"]150[/TD]
[/TR]
[TR]
[TD="align: center"]9[/TD]
[TD="align: center"]ccc[/TD]
[TD="align: center"]100[/TD]
[/TR]
[TR]
[TD="align: center"]10[/TD]
[TD="align: center"]ddd[/TD]
[TD="align: center"]50[/TD]
[/TR]
[TR]
[TD="align: center"]11[/TD]
[TD="align: center"]sss[/TD]
[TD="align: center"]250[/TD]
[/TR]
[TR]
[TD="align: center"]12[/TD]
[TD="align: center"]bbb[/TD]
[TD="align: center"]100[/TD]
[/TR]
</tbody>[/TABLE]


Criteria range for the filter is:

[TABLE="class: grid, width: 201"]
<tbody>[TR]
[TD="align: center"][/TD]
[TD="align: center"]A[/TD]
[/TR]
[TR]
[TD="align: center"]45[/TD]
[TD="align: center"]aaa[/TD]
[/TR]
[TR]
[TD="align: center"]46[/TD]
[TD="align: center"]ccc[/TD]
[/TR]
[TR]
[TD="align: center"]47[/TD]
[TD="align: center"]bbb[/TD]
[/TR]
</tbody>[/TABLE]


I am currently using a macro but that requires me to enter the range in the code:


Sub Test()
Dim Arr As Variant
Dim i As Integer
Arr = WorksheetFunction.Transpose(ActiveSheet.Range("A45:A47").Value)
For i = LBound(Arr) To UBound(Arr)
Arr(i) = CStr(Arr(i))
Next i
ActiveSheet.Range("$A$1:$A$12").AutoFilter Field:=1, Criteria1:=Arr, Operator:=xlFilterValues
End Sub


Can anyone help me create the macro where the 1st range is "selection" and criteria range is read from input box?

Thanks
 
Last edited:

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.
Not sure if I have understood you correctly, but try
Code:
Sub Test()
    Dim Arr As Variant
    Dim i As Integer
    Dim Rng As Range
    Dim Rws As Long
    
    Rws = InputBox("Please enter the number of criteria")
    Set Rng = Selection.Resize(Rws)
    Arr = WorksheetFunction.Transpose(Rng.Value)
    
    For i = LBound(Arr) To UBound(Arr)
        Arr(i) = CStr(Arr(i))
    Next i
    ActiveSheet.Range("$A$1:$A$12").AutoFilter Field:=1, Criteria1:=Arr, Operator:=xlFilterValues
End Sub
 
Upvote 0
Thanks fluff for the code. However, Rws = InputBox("Please enter the number of criteria") doesn't allow selection of range for the criteria range

Let me do a minor change in my requirement to make it simpler and understandable. I am looking for a code where the column to filter is column A (A:A) and criteria range is in some random place on the same sheet and can be selected and read by Input box.

Thanks
 
Upvote 0
If you select the first cell in the criteria range(A45, in your original post) & then enter the number of rows for the criteria in the input box (3 in you original post)then the code above should work.
Another option is you select the range where the criteria are (A45:A47, in your original post) & use this
Code:
Sub Test()
    Dim Arr As Variant
    Dim i As Integer
    
    Arr = WorksheetFunction.Transpose(Selection.Value)
    
    For i = LBound(Arr) To UBound(Arr)
        Arr(i) = CStr(Arr(i))
    Next i
    ActiveSheet.Range("$A$1:$A$12").AutoFilter Field:=1, Criteria1:=Arr, Operator:=xlFilterValues
End Sub
 
Upvote 0
Glad to help & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,224,825
Messages
6,181,190
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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