Decimal TryParse in VBA

2019-03-04 23:06发布

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

2条回答
我欲成王,谁敢阻挡
2楼-- · 2019-03-04 23:29

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

查看更多
时光不老,我们不散
3楼-- · 2019-03-04 23:31

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

查看更多
登录 后发表回答