Hi,

I have a userform with textboxes and the user can write expressions like 1+2 into the textboxes. I call Application.Evaluate to calulate the expression, get the result and process further. That works if the expression i valid, but if the user write an invalid expression into a textbox, the result is 0!

How can I catch that and get noticed that the expression can not be calculated?

Is there an official documentation available for that function? I can not find it.

Andreas.

Sub Test()

'Function Evaluate(ExpressionString As String, simpleEquation As Boolean, compileOnly As Boolean) As Double

' Element von SolidWorks.Application

' Evalutes ExpressionString using the VBA engine

Dim Expression As String

Dim Result As Double

'Works

Expression = "1+2*3"

Result = Application.Evaluate(Expression, True, False)

Debug.Print Result

'Invalid expression:

Expression = "x/12"

Result = Application.Evaluate(Expression, True, False)

'How can I know here that the expression could not be evaluated?

Debug.Print Result

End Sub

You could try adding 1 to the expression. If that still evaluates as zero, then the originial expression is not valid.

In your case, x / 12 is actually valid, since it gives x a value of zero, hence the end result is also zero.

However, if you try an expression like "1a" then that evaluates to zero, as does "1a + 1". So "1a" must be invalid.