Convert string to int if string is a number

前端 未结 6 1104
余生分开走
余生分开走 2020-12-05 06:08

I need to convert a string, obtained from excel, in VBA to an interger. To do so I\'m using CInt() which works well. However there is a chance that the string

6条回答
  •  無奈伤痛
    2020-12-05 06:52

    Here are a three functions that might be useful. First checks the string for a proper numeric format, second and third function converts a string to Long or Double.

    Function IsValidNumericEntry(MyString As String) As Boolean
    '********************************************************************************
    'This function checks the string entry to make sure that valid digits are in the string.
    'It checks to make sure the + and - are the first character if entered and no duplicates.
    'Valid charcters are 0 - 9, + - and the .
    '********************************************************************************
    Dim ValidEntry As Boolean
    Dim CharCode As Integer
    Dim ValidDigit As Boolean
    Dim ValidPlus As Boolean
    Dim ValidMinus As Boolean
    Dim ValidDecimal As Boolean
    Dim ErrMsg As String
    
    ValidDigit = False
    ValidPlus = False
    ValidMinus = False
    ValidDecimal = False
    
    ValidEntry = True
    For x = 1 To Len(MyString)
        CharCode = Asc(Mid(MyString, x, 1))
        Select Case CharCode
    
        Case 48 To 57 ' Digits 0 - 9
            ValidDigit = True
    
        Case 43 ' Plus sign
    
        If ValidPlus Then 'One has already been detected and this is a duplicate
            ErrMsg = "Invalid entry....too many plus signs!"
            ValidEntry = False
            Exit For
        ElseIf x = 1 Then 'if in the first positon it is valide
            ValidPlus = True
        Else 'Not in first position and it is invalid
            ErrMsg = "Invalide entry....Plus sign not in the correct position! "
            ValidEntry = False
            Exit For
        End If
    
        Case 45 ' Minus sign
    
        If ValidMinus Then 'One has already been detected and this is a duplicate
            ErrMsg = "Invalide entry....too many minus signs! "
            ValidEntry = False
            Exit For
        ElseIf x = 1 Then 'if in the first position it is valid
            ValidMinus = True
        Else 'Not in first position and it is invalid
            ErrMsg = "Invalide entry....Minus sign not in the correct position! "
            ValidEntry = False
            Exit For
        End If
    
        Case 46 ' Period
    
        If ValidDecimal Then 'One has already been detected and this is a duplicate
            ErrMsg = "Invalide entry....too many decimals!"
            ValidEntry = False
            Exit For
        Else
            ValidDecimal = True
        End If
    
        Case Else
            ErrMsg = "Invalid numerical entry....Only digits 0-9 and the . + - characters are valid!"
            ValidEntry = False
            Exit For
    
        End Select
    
    Next
    
        If ValidEntry And ValidDigit Then
            IsValidNumericEntry = True
        Else
            If ValidDigit = False Then
                ErrMsg = "Text string contains an invalid numeric format." & vbCrLf _
                & "Use only one of the following formats!" & vbCrLf _
                & "(+dd.dd  -dd.dd  +dd  -dd  dd.d or dd)! "
            End If
            MsgBox (ErrMsg & vbCrLf & vbCrLf & "You Entered:   " & MyString)
            IsValidNumericEntry = False
        End If
    
    End Function
    
    Function ConvertToLong(stringVal As String) As Long
    'Assumes the user has verified the string contains a valide numeric entry.
    'User should call the function IsValidNumericEntry first especially after any user input
    'to verify that the user has entered a proper number.
    
     ConvertToLong = CLng(stringVal)
    
    
    End Function
    Function ConvertToDouble(stringVal As String) As Double
    'Assumes the user has verified the string contains a valide numeric entry.
    'User should call the function IsValidNumericEntry first especially after any user input
    'to verify that the user has entered a proper number.
    
        ConvertToDouble = CDbl(stringVal)
    
    End Function
    

提交回复
热议问题