gpt4 book ai didi

string - 将 VBA 字符串转换为 double

转载 作者:行者123 更新时间:2023-12-03 14:58:01 33 4
gpt4 key购买 nike

我正在使用非常基本的 VBA 编码来创建一个模板,该模板从 Windows 中的其他屏幕中提取数据。当它提取数字时,它们被格式化为字符串。我现在需要将字符串转换为 double ,以便添加/减去它们。我一直在尝试一切,但似乎无法弄清楚。

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

关于string - 将 VBA 字符串转换为 double ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42493896/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com