Wayne Hawkes
asked on
How do I use a value from an input box to be used as a text filter in a table later in a macro?
This may be an obvious question to many, but I've tried multiple iterations and much internet searching and can't get this to work. I basically want to ask the user to input a value and then use that value in a table with filters to filter the data using the input word. Would appreciate any help. My current attempt below.
Thanks
Thanks
Dim newVal
newVal = InputBox("Enter the search term")
If newVal = "" Then Exit Sub
Sheets("Question Database").Select
ActiveSheet.ListObjects("RFPQs").Range.AutoFilter Field:=1, Criteria1:= _
" & newVal & ", Operator:=xlAnd
And if the value is expected to be enclosed within double quotes, it should be:
ActiveSheet.ListObjects("RFPQs").Range.AutoFilter Field:=1, Criteria1:= """" & newVal & """", Operator:=xlAnd
ASKER
Thanks for the suggestion. Just gave it a go, but unfortunately it doesn't work.
One more suggestion, to be sure that the problem isn't elsewhere, you should also try with a constant value and make sure it works perfectly, first. As in:
The above must work first. Otherwise, there is a problem somewhere else and must be fixed first.
ActiveSheet.ListObjects("RFPQs").Range.AutoFilter Field:=1, Criteria1:= "somevalue", Operator:=xlAnd
The above must work first. Otherwise, there is a problem somewhere else and must be fixed first.
ASKER
It definitely works with a constant value. Tried with the double quotes and still doesn't work unfortunately. It's a mystery to me!
Hummm...Running short of ideas. Overwrite the variable value with the constant value, bypassing the value received from InputBox, as in
The essential aspect of such exercise is to isolate the problem. And keep removing as many variables in the problem as possible.
If newVal = "" Then Exit Sub
newVal = "somevalue" 'This must be the one which you already tried as constant value.
The essential aspect of such exercise is to isolate the problem. And keep removing as many variables in the problem as possible.
What are you expecting NewVal to return, i.e. what are you filtering - dates or values?
ASKER
I am trying to filter a database / list of questions. As an example, there may be 1000 questions in the list. I would like the user to be able to enter a search term e.g. Fees or Team and then for the macro to use this term to put a custom filter on the list and show only those with the search term within them. My macro then copies this shortened list to a new tab. It works fine with a fixed value, just can't find a way to substitute in a value from an input box. I'm very new to this though.
ASKER
As a further explanation, here is the code that works:
Sheets("Question Database").Select
ActiveSheet.ListObjects("R FPQs").Ran ge.AutoFil ter Field:=1, Criteria1:= _
"=*Fee*", Operator:=xlAnd
I want to replace Fee with the value form the input box.
Thanks.
Sheets("Question Database").Select
ActiveSheet.ListObjects("R
"=*Fee*", Operator:=xlAnd
I want to replace Fee with the value form the input box.
Thanks.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Ryan, you are a genius! Just tried that and it works beautifully. Thank you very much for your help.
ASKER
Ryan's suggestion worked perfectly. Thanks to Ryan and all other contributors for their help.
You are not building an expression here so I don't really understand the context of the " and & here. I believe, you should just put the variable name next to Criteria1 as in:
Open in new window
Have you tried that already?