将 VBA 字符串转换为 Double

问题描述 投票:0回答:3

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

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

当我这样做时,我收到以下错误:

编译错误:变量未定义,并且突出显示第一个 转换功能。

string vba type-conversion double
3个回答
25
投票

您使用了错误的函数进行转换。你需要使用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

3
投票

这是基于我的其他答案

如果允许用户使用其他字符(例如,

$
符号),那么下面的函数可能会很有用(基本上,Guilherme的答案的故障安全版本):

'
' Skips all characters in the input string, except,
' the first negative-sign, digits, and the first dot.
'
Function ParseNumber(ByVal s As String) As Double
    ParseNumber = 0#
    Dim char As String
    Dim i As Integer
    Dim digits$
    Dim isNegative As Boolean
    Dim isPastDot As Boolean
    For i = 1 To Len(s)
        char = Mid(s, i, 1)
        If char >= "0" And char <= "9" Then
            digits = digits & char
        ElseIf char = "-" Then
            If Len(digits) <= 0 Then
                isNegative = True
            End If
        ElseIf char = "." Then
            If Not isPastDot Then
                isPastDot = True
                digits = digits & "."
            End If
        End If
    Next i
    ParseNumber = CDbl(digits)
    If isNegative Then
        ParseNumber = 0 - ParseNumber
    End If
End Function

-2
投票

看到这一点以及这些解决方案是多么冗长,这让人很伤心!我希望这对某人有帮助...

Dim str_Impl_Vol As String ' let's say 37.9% is the string

Dim str_Impl_Vol = Left(ActiveSheet.Cells(intCurrentRow, intCol_Impl_Vol), 
                   Len(ActiveSheet.Cells(intCurrentRow, intCol_Impl_Vol)) - 1)
                   ' Remove the %

Dim dbl_Impl_Vol As Double ' let's say we need 37.9% as 0.379

dbl_Impl_Vol = CDbl(str_Impl_Vol / 100) ' just do it!

有什么问题吗?

© www.soinside.com 2019 - 2024. All rights reserved.