How to use square brackets for a string evaluation in VBA?

人盡茶涼 提交于 2019-12-06 02:29:54

问题


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.



来源:https://stackoverflow.com/questions/50132024/how-to-use-square-brackets-for-a-string-evaluation-in-vba

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!