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.


AB
1AccountValue
2bbb150
3aaa100
4ccc200
5aaa150
6bbb250
7vvv200
8aaa150
9ccc100
10ddd50
11sss250
12bbb100

<tbody>
</tbody>


Criteria range for the filter is:

A
45aaa
46ccc
47bbb

<tbody>
</tbody>


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

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.
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,214,657
Messages
6,120,777
Members
448,991
Latest member
Hanakoro

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