How to use square brackets for a string evaluation

2020-07-11 11:42发布

问题:

Using square brackets for string evaluation, I am trying to make [inputString] return 320. Any ideas how to do it?

Public Sub TestMe()

    Dim inputString As String
    inputString = "20+300"
    Debug.Print Application.Evaluate(inputString)   'ok
    Debug.Print Evaluate([inputString])             'ok
    Debug.Print [20+300]                            'ok
    Debug.Print ["20"+"300"]                        'ok
    Debug.Print ["20"+300]                          'ok
    Debug.Print [inputString]                       'returns "20+300" and not 320

End Sub

回答1:

From the link in the comment: "Using square brackets (for example, "[A1:C5]") is identical to calling the Evaluate method with a string argument."

I read that to mean:

Evaluate("20+10") is equivalent to [20+10] where the Evaluate function takes the string and converts to a literal interpretation before evaluating.

but ["20+10"] is just equivalent to evaluating a string "20+10"

The reason Debug.Print ["20"+"300"] works is because VBA is great at auto-converting "20" to 20.



标签: vba excel eval