Link to home
Start Free TrialLog in
Avatar of Wayne Hawkes
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

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

Open in new window

Avatar of Nitin Sontakke
Nitin Sontakke
Flag of India image

Disclaimer first, I am not much of expert here but commenting from just the common sense point of view. Feel free to ignore.

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:

ActiveSheet.ListObjects("RFPQs").Range.AutoFilter Field:=1, Criteria1:= newVal, Operator:=xlAnd

Open in new window


Have you tried that already?
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

Open in new window

Avatar of Wayne Hawkes
Wayne Hawkes

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:

ActiveSheet.ListObjects("RFPQs").Range.AutoFilter Field:=1, Criteria1:= "somevalue", Operator:=xlAnd

Open in new window


The above must work first. Otherwise, there is a problem somewhere else and must be fixed first.
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

If newVal = "" Then Exit Sub

newVal = "somevalue"  'This must be the one which you already tried as constant value.

Open in new window


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?
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.
As a further explanation, here is the code that works:

Sheets("Question Database").Select
    ActiveSheet.ListObjects("RFPQs").Range.AutoFilter Field:=1, Criteria1:= _
        "=*Fee*", Operator:=xlAnd

I want to replace Fee with the value form the input box.

Thanks.
ASKER CERTIFIED SOLUTION
Avatar of Ryan Chong
Ryan Chong
Flag of Singapore image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Ryan, you are a genius!  Just tried that and it works beautifully.  Thank you very much for your help.
Ryan's suggestion worked perfectly.   Thanks to Ryan and all other contributors for their help.