(Solved) – Evaluate() not working with brackets in VBA

(solved)-–-evaluate()-not-working-with-brackets-in-vba

Trying to make the following work:

Sub Test()

    Dim condition As String
    Dim comparison As String

    condition = "({0} > {1} And ({1} = 2 Or {2} <> 3))"

    Dim variable As String
    variable = StringFormat(condition, 3, 2, 4)

    Debug.Print variable
    Dim result As Variant
    result = Evaluate(variable)
    Debug.Print result

End Sub

Public Function StringFormat(mask As String, ParamArray tokens()) As String

    Dim i As Long
    For i = LBound(tokens) To UBound(tokens)
        mask = Replace$(mask, "{" & i & "}", tokens(i))
    Next
    StringFormat = mask

End Function

and somehow this is what I got in the immediate window:

enter image description here

which is rather ok, but when I try to evaluate the first line I get the result I am expecting:

enter image description here
Changing the condition to condition = "({0} > {1})" works.

Any idea how to evaluate this: ({0} > {1} And ({1} = 2 Or {2} <> 3)) with the Evaluate() method?

Leave a Reply

Your email address will not be published. Required fields are marked *