Autofilter from input box

dan_pafc

Board Regular
Joined
Aug 16, 2007
Messages
162
Hi

Im looking to filter a column based on a code the user puts into an input box.

I know the first part of the code should be ok but the second i have just entered, if anyone can help!

Sub CodeFilter()

Dim oS, cl As Range
oS = Application.InputBox("Please Enter Search Criteria")
If oS = "" Then MsgBox "You must Enter a Code": Exit Sub
If oS = "False" Then Exit Sub

For Each cl In Range
Rows("1:1").Select
Selection.AutoFilter Field:=5, Criteria1:="oS", Operator:=xlAnd

End Sub
 

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
I now have it down to the following which selects row 1 but doesnt filter:

Sub CodeFilter()

Dim oS, cl As Range
oS = Application.InputBox("Please Enter Search Criteria")
If oS = "" Then MsgBox "You must Enter a Code": Exit Sub
If oS = "False" Then Exit Sub

Rows("1:1").Select
Selection.AutoFilter Field:=5, Criteria1:=oS, Operator:=xlAnd

End Sub
 
Upvote 0
If you look at the following:

dim strInput as string
strinput = Inputbox("Enter your value to filter on")

then when your ready for the filter code add the strInput as the criteria.

Sub mcrFilter()
Dim strInput As String
strInput = InputBox("Enter your value to filter on")
Selection.AutoFilter
ActiveSheet.Range("$A$1:$K$9359").AutoFilter Field:=1, Criteria1:= _
strInput
End Sub
 
Upvote 0
If you look at the following:

dim strInput as string
strinput = Inputbox("Enter your value to filter on")

then when your ready for the filter code add the strInput as the criteria.

Sub mcrFilter()
Dim strInput As String
strInput = InputBox("Enter your value to filter on")
Selection.AutoFilter
ActiveSheet.Range("$A$1:$K$9359").AutoFilter Field:=1, Criteria1:= _
strInput
End Sub


Thanks for that Trevor - i need to use the contains strInput - everything I input doesnt work. Any ideas?
 
Upvote 0
Change the column number in my example to 5 which is indicated in yours then it should work
 
Upvote 0
Dan,

You could use the * to search something like this:

*Tea* which would find anything that has text before Tea and also anything after.

Regards

Trevor
 
Upvote 0
Code:
ActiveSheet.Range("$A$1:$K$9359").AutoFilter Field:=5, Criteria1:= "*" & strInput & "*", Operator:=xlAnd
 
Upvote 0
You could also try:
Code:
Columns("E").AutoFilter Field:=1, Criteria1:="*" & strInput & "*"
 
Upvote 0

Forum statistics

Threads
1,214,590
Messages
6,120,423
Members
448,961
Latest member
nzskater

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