如何在VBA中使用方括号进行字符串评估? [英] How to use square brackets for a string evaluation in VBA?

查看:130
本文介绍了如何在VBA中使用方括号进行字符串评估?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用方括号进行字符串评估,我试图使 [inputString] 返回 320
有什么想法吗?

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


推荐答案

来自注释中的链接:使用方括号(例如, [A1:C5])等同于使用字符串参数调用Evaluate方法。

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

我的意思是:

Evaluate( 20 + 10) [20 + 10] ,其中求值函数将字符串取值并转换为文字解释,然后求值。

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

[ 20 + 10 ] 等效于评估字符串 20 + 10

Debug.Print [ 20 + 300] 起作用的原因是因为VBA擅长将 20自动转换为20。

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

这篇关于如何在VBA中使用方括号进行字符串评估?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆