VBA Evaluate function with string arguments

Mats Olsson picture Mats Olsson · Feb 27, 2015 · Viewed 13.3k times · Source

I have this function working ( It returns the row where the text DK001 sits in the ID range)

Found = Application.Evaluate("=IF(ID=""DK001"",ROW(ID),""x"")")

I would like to feed the searchcriteria (e.g. DK001) as a string, like

Found = Application.Evaluate("=IF(ID=SearchString,ROW(ID),""x"")")

I fail in creating a string that is accepted as a search criteria. I need your help on this! What am I doing wrong?


This Evaluate function is haunting me ....

What if I now wanted to send a value (not a string) to the function?

Found = Application.Evaluate("=IF(ID=1,ROW(ID),""x"")")

The above works!

But if I want this to be a variable like

Found = Application.Evaluate("=IF(ID=MyValue,ROW(ID),""x"")")

What then?

Answer

Alex K. picture Alex K. · Feb 27, 2015

Double " to include them as literals:

SearchString = "DK001"
Found = Application.Evaluate(""=IF(ID=""" & SearchString & """,ROW(ID),""x"")")