• Hex to Decimal (VBA/Excel/XP)

    Author
    Topic
    #407235

    I want to convert Hex values to Decimal in VBA. The builtin function HEX2DEC apparently does not work inside a module. Example change EE to 238

    Viewing 4 reply threads
    Author
    Replies
    • #850268

      I noticed that VBA has Hex(), Hex$(), Oct() and Oct$() functions. Seems they forgot about going the other direction. sad

      • #850279

        There is probably no need as x = &HFF will assign the value 255 to x. The Bin, Oct & Hex functions just return an appropriate string representation of the value passed to them.

        Andrew

      • #850280

        There is probably no need as x = &HFF will assign the value 255 to x. The Bin, Oct & Hex functions just return an appropriate string representation of the value passed to them.

        Andrew

    • #850222

      Say that your hex value is stored in a variable strHex.

      CLng(“&H” & strHex)

      will yield the decimal value. So you can create your own Hex2Dec:

      Function Hex2Dec(HexVal As Variant) As Variant
      Hex2Dec = “”
      On Error Resume Next
      Hex2Dec = CLng(“&H” & strHex)
      End Function

      This function will return a blank if the input is not a valid hex string. If you’d rather return 0 or Null, change the second line accordingly.

    • #850223

      Say that your hex value is stored in a variable strHex.

      CLng(“&H” & strHex)

      will yield the decimal value. So you can create your own Hex2Dec:

      Function Hex2Dec(HexVal As Variant) As Variant
      Hex2Dec = “”
      On Error Resume Next
      Hex2Dec = CLng(“&H” & strHex)
      End Function

      This function will return a blank if the input is not a valid hex string. If you’d rather return 0 or Null, change the second line accordingly.

    • #850309

      CDec() works as well:

      ?hex(1234)
      4D2
      ?cdec(&H4D2)
      1234 
      

      (From the Immediate window)

      • #850317

        It will, but (a) VBA does not support variables of type decimal, so you might as well use CLng, and ( you still have to prefix the value with &H. CDec doesn’t recognize a hex string such as 3F by itself.

        • #851801

          Clng is a great instruction. Some idiot set this machine up without help.

        • #851802

          Clng is a great instruction. Some idiot set this machine up without help.

      • #850318

        It will, but (a) VBA does not support variables of type decimal, so you might as well use CLng, and ( you still have to prefix the value with &H. CDec doesn’t recognize a hex string such as 3F by itself.

    • #850310

      CDec() works as well:

      ?hex(1234)
      4D2
      ?cdec(&H4D2)
      1234 
      

      (From the Immediate window)

    Viewing 4 reply threads
    Reply To: Hex to Decimal (VBA/Excel/XP)

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: