Search code examples
excelvbaevaluate

Evaluating Greater Than x But Less Than x VBA


I am building an application which must interpret a set of instructions set by the user, instructions which will not be known until runtime.

I am achieving this by using the Evaluate function, as so:

Evaluate("5>4")

Which returns True, as one would expect. My question is how do I evaluate the following statement:

5 is greater than 4, but less than 10

I will be substituting the numbers with variables, of course.

I realise I could split the string into an array of two parts and test individually, but there must be a way of passing a single argument to test.


Solution

  • Application.Evaluate evaluates Formulas so AND(5>A4,5<10) or (5>A4)*(5<10) (results in 0 or 1)

    Another alternative could be ScriptControl Eval, but it can't access Excel addresses like Evaluate

    With CreateObject("ScriptControl")
        .Language = "VBScript"              ' either VBScript or JScript
        Debug.Print .Eval("5>4 and 5<10")
    End With