Decimal TryParse in VBA

老子叫甜甜 提交于 2019-12-02 19:57:30

问题


I am attempting to tryparse using decimal; however, I keep getting an "Object Required" run-time error. I'm not certain what I'm doing wrong. I'm used to doing a tryparse in C#. This is VBA, so the language translation is not clicking just yet. Any help appreciated.

 Sub try()
        Dim val As Variant
        Dim res As Boolean

        res = Decimal.TryParse("2.5", val)
        MsgBox (res & ":" & val)
    End Sub

回答1:


You can try CInt and check for a specific error using On Error Goto.




回答2:


res = cBool(Val("2.5")) should do the trick here, since any value <> 0 will evaluate as True



来源:https://stackoverflow.com/questions/23655251/decimal-tryparse-in-vba

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