将VBA字符串转换为双精度值
问题描述:
我正在使用VBA的基本编码来创建一个模板,用于从Windows中的其他屏幕提取数据。当它拉出数字时,它们被格式化为字符串。我现在需要将字符串转换为双精度,以便添加/减去它们。我一直在尝试一切,但似乎无法弄清楚。将VBA字符串转换为双精度值
Me.salesprice = Trim(scrn.GetString(11, 65, 10))
'This would be formatted as 25,000.00
Me.salestax = Trim(scrn.GetString(12, 66, 10))
Me.pastdue = Trim(scrn.GetString(14, 65, 10))
Me.assessedppt = Trim(scrn.GetString(18, 66, 10))
Me.secdep = Trim(scrn.GetString(17, 65, 10))
assessedppt = Convert.ToDouble(Me.assessedppt)
uappt = Convert.ToDouble(Me.uappt)
salesprice = Convert.ToDouble(Me.salesprice)
salestax = Convert.ToDouble(Me.salestax)
pastdue = Convert.ToDouble(Me.pastdue)
lc = Convert.ToDouble(frmDetails.lc)
totalfinance = salesprice + salestax + pastdue - secdep + assessedppt + uappt + lc
totalsalesprice = salesprice + pastdue
ppt = assessedppt + uappt
当我这样做,我得到以下错误:
Compile error: Variable not defined, and it highlights the first Convert function.
答
你使用了错误的功能转换。您需要使用CDbl,在VBA我们有遵循转换功能:
numberDouble = CDbl("10") 'For convert to double
numberInteger = CInt("12") 'For convert to Integer
varString = CStr("11") 'For convert to String
bool = CBool("true") 'For convert to Boolean
所以,如果你改变你的Convert.toDouble,你的代码将看起来像这样:
Me.salesprice = Trim(scrn.GetString(11, 65, 10))
'This would be formatted as 25,000.00
Me.salestax = Trim(scrn.GetString(12, 66, 10))
Me.pastdue = Trim(scrn.GetString(14, 65, 10))
Me.assessedppt = Trim(scrn.GetString(18, 66, 10))
Me.secdep = Trim(scrn.GetString(17, 65, 10))
assessedppt = CDbl(Me.assessedppt.value)
uappt = CDbl(Me.uappt.value)
salesprice = CDbl(Me.salesprice.value)
salestax = CDbl(Me.salestax.value)
pastdue = CDbl(Me.pastdue.value)
lc = CDbl(frmDetails.lc.value)
totalfinance = salesprice + salestax + pastdue - secdep + assessedppt + uappt + lc
totalsalesprice = salesprice + pastdue
ppt = assessedppt + uappt
+0
还有更多[转换功能](https://msdn.microsoft.com/en-us/library/office/gg278896.aspx)比提到的4,但我想他们是主要的。 – YowE3K
'Convert.ToDouble'是VB.NET,这是一种完全不同的语言。在VBA中,函数是'CDbl'。 – Comintern