Convert VBA String to Double Convert VBA String to Double vba vba

Convert VBA String to Double


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

numberDouble = CDbl("10") 'For convert to doublenumberInteger = CInt("12") 'For convert to IntegervarString = CStr("11") 'For convert to Stringbool = CBool("true") 'For convert to Boolean

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

Me.salesprice = Trim(scrn.GetString(11, 65, 10))'This would be formatted as 25,000.00Me.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 + lctotalsalesprice = salesprice + pastdueppt = assessedppt + uappt


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):

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


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 stringDim 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.379dbl_Impl_Vol = CDbl(str_Impl_Vol / 100) ' just do it!

Any questions?