1

So I am using Instr with Evaluation and facing some difficulties

The code is as follows

 myIneq=">"
 myString1="Hello"
 myString2="el"
 Evaluate( "Instr(" & myString1 & "," & myString2 & ")" & myIneq & cstr(0)

I am getting an Error 2029. Based off this msdn link I am assuming it is trying to evaluate "Hello" as a variable name. What is the work around for this, I know there must be one.

Thanks

5
  • 2
    Why are you using Evaluate?
    – SLaks
    Oct 10, 2011 at 23:01
  • this is a more generic example of what i am trying to od
    – jason m
    Oct 10, 2011 at 23:40
  • This is a bad security hole. Whatever you're trying to do, there are better ways to do it.
    – SLaks
    Oct 11, 2011 at 0:12
  • I would be glad to know a better way. It is a function that will allow a user to filter a data set. The user will say if a field may or may not contain a string. That's it. The easiest way I thought to do this was with instr and modifying the inequality.
    – jason m
    Oct 11, 2011 at 3:29
  • O.K., seeing your comment here, there probably is a better way to do what you really want. If this is all VBA code, why can't you just call Instr directly? Why generate code and evaluate it? But I'm glad my answer helped with your immediate issue.
    – jtolle
    Oct 11, 2011 at 14:26

3 Answers 3

1

I infer from the Error 2029 (#NAME?) and the link that you're using Excel. In this case the answer is simple. Application.Evaluate evaluates Excel expressions, not VBA code. That is, any functions you call in your expression have to be things you could call from an Excel formula. (And you're correct that Excel is trying to evaluate the value of a symbol it doesn't recognize, and is thus giving you back a #NAME? error.)

There is an Excel worksheet function, FIND, that does pretty much the same thing that the VBA function Instr does, so if your example is not too simplified, that might be all you need to do.

I just typed this into the Immediate window:

x="Hello"
y="el"
?Evaluate("FIND(""" & y & """, """ & x & """)")
 2 

ineq = ">"

?Evaluate("FIND(""" & y & """, """ & x & """)" & ineq & "0")
True

and it seems to work.

Note that Evaluate is a function, so it expects to receive a string argument, and then return what that string evaluates to if treated as an Excel formula-syntax expression. In your example, you don't seem to be doing anything with the return value, so I thought I'd mention it.

0
1

"Evaluate" doesn't understand all excel functions.

For example, trying to evaluate "instr" will give an Error 2029. But there is a nice workaround:

  • "evaluate" recognizes all added vba functions of your excel sheet
  • so just wrap a single-line function around the reluctant function.

Code will be similar to this:

Sub test()
   MsgBox Evaluate(" Instring(""Hello"",""el"")  ")
   Msgbox "\o/ ! ... I owe a beer to someone out there"
End Sub

Function Instring(a, b)
   'make instr visible to 'evaluate'
   Instring = InStr(a, b)
End Function
0

You're evaluating the string InStr(Hello,el).
Obviously, that's not what you want.

You need to use a quoted string literal.

2
  • this is failing as well: "Instr(" & """ & dictFile(Key1)(strField) & """ & _ "," & """ & strFilter & """ & ")"
    – jason m
    Oct 10, 2011 at 23:30
  • @jason: How does it look if you Debug.Print it? Oct 11, 2011 at 0:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.