VBcoders Guest



Don't have an account yet? Register
 


Forgot Password?



Convert any Numerical String Format to valid numerical string and set it to be ready to be converte

by Francois [MageMinds] Allard (1 Submission)
Category: String Manipulation
Compatability: VB 6.0
Difficulty: Unknown Difficulty
Originally Published: Tue 14th March 2000
Date Added: Mon 8th February 2021
Rating: (1 Votes)

Convert any Numerical String Format to valid numerical string and set it to be ready to be converted with CDbl or something like that.

Rate Convert any Numerical String Format to valid numerical string and set it to be ready to be converte



    Dim dblTstErr As Double
    Dim Ctr As Integer, Nb As Integer
    On Error Resume Next
    Nb = 0
    For Ctr = 1 To Len(Txt)
        If Mid(Txt, Ctr, 1) = "." Then
            Nb = Nb + 1
        End If
    Next
    If Nb > 1 Or Nb = 0 Then
        Nb = 0
        For Ctr = 1 To Len(Txt)
            If Mid(Txt, Ctr, 1) = "," Then
                Nb = Nb + 1
            End If
        Next
    End If
    If Nb > 1 Then
        Nb = 0
        For Ctr = 1 To Len(Txt)
            If Mid(Txt, Ctr, 1) = "," Then
                Nb = Nb + 1
            End If
        Next
        If Nb > 1 Then
            Nb = 0
            For Ctr = 1 To Len(Txt)
                If Mid(Txt, Ctr, 1) = "." Then
                    Nb = Nb + 1
                End If
            Next
        End If
    End If
    If Nb = 1 Or Nb = 0 Then
        SwitchDot = Txt
        dblTstErr = CDbl(SwitchDot)
        If Err <> 0 Then
            Err = 0
            SwitchDot = Replace(Txt, ",", ".")
            dblTstErr = CDbl(SwitchDot)
            If Err <> 0 Then
                Err = 0
                If InStr(1, Txt, ".") > 0 Then
                    SwitchDot = Replace(Txt, ",", " ")
                    dblTstErr = CDbl(SwitchDot)
                    If Err <> 0 Then
                        Err = 0
                        SwitchDot = Replace(SwitchDot, ".", ",")
                        dblTstErr = CDbl(SwitchDot)
                        If Err <> 0 Then
                            SwitchDot = "0"
                        End If
                    End If
                End If
                If Err <> 0 Then
                    Err = 0
                    If InStr(1, Txt, ",") > 0 Then
                        SwitchDot = Replace(Txt, ".", " ")
                        dblTstErr = CDbl(SwitchDot)
                        If Err <> 0 Then
                            Err = 0
                            SwitchDot = Replace(SwitchDot, ",", ".")
                            dblTstErr = CDbl(SwitchDot)
                            If Err <> 0 Then
                                SwitchDot = "0"
                            End If
                        End If
                    End If
                End If
            End If
        End If
    Else
        SwitchDot = "0"
    End If
End Function


Download this snippet    Add to My Saved Code

Convert any Numerical String Format to valid numerical string and set it to be ready to be converte Comments

No comments have been posted about Convert any Numerical String Format to valid numerical string and set it to be ready to be converte. Why not be the first to post a comment about Convert any Numerical String Format to valid numerical string and set it to be ready to be converte.

Post your comment

Subject:
Message:
0/1000 characters