Question

I am experiencing this runtime error - type mismatch on the following function call in VBA

Dim sTest As String
sTest = "=Search(" & Chr(34) & "MyString" & Chr(34) & Chr(44) & "A2" & ")>0"
''the above evaluates to =Search("MyString",A2)>0
Application.Evaluate(sTest)

So the evaluate function throws a runtime error-type mismatch

I tried

sTest = "=Search(" & Chr(34) & "MyString" & Chr(34) & Chr(44) & Chr(34) "Test MyString" & Chr(34) ")>0"

Still the same error

When I use the above string in the excel formula bar, it works!!

I tried

sTest = "=Search(""MyString"", ""Test MyString"")>0"

It Worked in VBA

To My Surprise

sTest = "=Search(" & Chr(34) & Chr(34) & "MyString" & Chr(34) & Chr(34) & Chr(44) & Chr(34) & Chr(34) & "Test MyString" & Chr(34) & Chr(34) & ")>0"

Din work

Could somebody help here why the first block of code does not work?

Was it helpful?

Solution

If you want to include a quote in a string all you need to do is double it up:

sTest = "=Search(""MyString"",A2)>0"

to me that's easier than using Chr(34)

Note that SEARCH() returns an error if the string was not found, not 0.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top