Results 1 to 6 of 6

Thread: Character to number

  1. #1
    VBAX Newbie
    Joined
    Feb 2020
    Posts
    1
    Location

    Question Character to number

    Hello guys,

    I'm having a little issue right now with a function. When the user enter a caracter, I need to convert this caracter in number.


    Function Convert(ByVal Value As String) As Integer
    If Value= "A" Then
        Convert = 10
    ElseIf Value = "B" Then
        Convert = 11
    ElseIf Value = "C" Then
        Convert = 12
    ElseIf Value = "D" Then
        Convert = 13
    ElseIf Value = "E" Then
        Convert = 14
    ElseIf Value = "F" Then
        Convert = 15
    Else: Convert = Value
    End If
    End Function

    Thank you

  2. #2
    Moderator VBAX Wizard Aussiebear's Avatar
    Joined
    Dec 2005
    Location
    Queensland
    Posts
    5,391
    Location
    Try a Case select method
    Function Convert(ByVal Value As String) As Integer
        Select Case Value
            Case "A"
                Convert = 10
            Case "B"
                Convert = 11
            Case "C"
                Convert = 12
            Case "D"
                Convert = 13
            Case "E"
                Convert = 14
            Case "F"
                Convert = 15
            Case Else
                  ' Assuming Value should be converted to an integer if it's not A-F
                  ' You might want to add error handling here if Value isn't always a valid number
                  If IsNumeric(Value) Then
                      Convert = CInt(Value)
                  Else
                       ' Handle the case where Value is not A-F and not a number
                       ' For example:                
                       Convert = 0 
                       ' Or raise an error, or return a specific error code
                  End If
            End Select
    End Function
    Remember To Do the Following....
    Use [Code].... [/Code] tags when posting code to the thread.
    Mark your thread as Solved if satisfied by using the Thread Tools options.
    If posting the same issue to another forum please show the link

  3. #3
    VBAX Mentor
    Joined
    Nov 2022
    Location
    The Great Land
    Posts
    413
    Location
    Doubt OP is still hanging around looking for answer so we'll never know if they found resolution. And their code works anyway except for case sensitivity. Maybe that's what they were dealing with.

    Instead of If Then or Select Case, could:

    Convert = Asc(UCase(Value)) - 55

    Could even calc in query or textbox without VBA.
    Last edited by June7; 04-06-2025 at 11:22 PM.
    How to attach file: Reading and Posting Messages (vbaexpress.com), click Go Advanced below post edit window. To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  4. #4
    VBAX Mentor
    Joined
    Nov 2022
    Location
    The Great Land
    Posts
    413
    Location
    Oops, disregard my comment about case sensitivity. Unfortunately, can no longer edit my earlier post.
    How to attach file: Reading and Posting Messages (vbaexpress.com), click Go Advanced below post edit window. To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  5. #5
    Public Function Convert(Byval Value As String) As Integer
    On Error Resume Next
    Convert = CInt("&H" & Value)
    End Function

  6. #6
    VBAX Mentor
    Joined
    Nov 2022
    Location
    The Great Land
    Posts
    413
    Location
    Nice, arnel. And again, can be expression in query or textbox.
    How to attach file: Reading and Posting Messages (vbaexpress.com), click Go Advanced below post edit window. To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •