将 VBA 字符串转换为双精度

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42493896/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 10:45:35  来源:igfitidea点击:

Convert VBA String to Double

stringvbatype-conversiondouble

提问by c.olson

I am using the very basic coding of VBA for word to create a template that pulls data from other screens in windows. When it pulls the numbers, they are formatted as strings. I now need to get the strings to be converted to doubles, in order to add/ subtract them. I have been trying everything but cannot seem to figure it out.

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

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

When I do this, I get the following error:

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

Compile error: Variable not defined, and it highlights the first Convert function.

编译错误:变量未定义,它突出显示第一个 Convert 函数。

回答by Guilherme Felipe Reis

You're using the wrong function to convert. You need to use CDbl, in VBA we have the follow convert functions:

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

So if you change your Convert.toDouble, your code will looks like that:

因此,如果您更改 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

回答by Top-Master

This is based on my other answer:

这是基于我的另一个答案

In case the user is allowed to use other characters (for example, the $sign), then the below function could be useful (in combination of Guilherme Felipe Reis's answer):

如果允许用户使用其他字符(例如,$符号),则以下功能可能很有用(结合Guilherme Felipe Reis 的回答):

'
' 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

回答by Darrell Kolodka

This hurts to see this and how verbose these solutions are! I hope this helps someone...

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

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!

Any questions?

任何问题?