Hephaistos
Hephaistos

Reputation: 51

Evaluate("1") gives error 438

I have few "Why?"s about evaluate...

Sub TestEvaluate()
Dim Tag As String
Tag = "5"
Tag = Evaluate(Tag) 'works fine

Tag = "1"
Tag = Evaluate(Tag) 'error 438 wrong property or method(-> my 1st "Why?")

But ok i can handle it:

Tag = "1"
Tag = [Tag] 'works fine

Now I need to evaluate a property of some object:

Dim Object As cObject
Set Object = New cObject
Object.pProperty = "5"
Tag = Evaluate(Object.pProperty) 'Works fine

And again the same problem as above:

Object.pProperty = "1"
Tag = Evaluate(Object.pProperty) '438 wrong property or method

But now i'm traped, becouse:

Tag = [Object.pProperty] 'generates error 13 type mismatch(-> my 2nds "Why?")

Is there some solution without the need to use a new variable?

Dim TempTag As String
TempTag = Object.pProperty
Tag = [TempTag] 'everything fine again

End Sub

i found out, in my case VBA.Evaluate("1") generates an object according to

debug.print VBA.VarType(evauate("1"))

It`s just a bug? (win8.1 xl2007)

Upvotes: 2

Views: 743

Answers (3)

nhannt210695
nhannt210695

Reputation: 89

I use an equals sign (=) in the "Name" parameter string:

Trim(Evaluate("=1")) ' The result is 1.
Trim(Evaluate(" =1")) ' In VBA, Run-time error '13': Type mismatch. But not when string from a cell of Excel.
Trim(Evaluate("=1 ")) ' The result is 1.
Evaluate(" =1") ' Error 2015.
Trim(Evaluate("1")) ' Run-time error '438': Object doesn't support this property or method.
Evaluate("1") ' Run-time error '438': Object doesn't support this property or method.

I use this In Excel VBA, I use it like this

In VBA, Run-time error '13': Type mismatch. But not when string from a cell of Excel.

Upvotes: 0

Jan
Jan

Reputation: 1

This is better solution for me:

Tag = Evaluate(Tag & "+0")

It is a solution for the Error 2015 when the Tag="" as well.

Upvotes: 0

Hephaistos
Hephaistos

Reputation: 51

I put the expression into brackets and the problem disappeared; works fine now:

Tag = Evaluate("(" & Tag & ")")

Upvotes: 3

Related Questions