# Thread: Hex to Decimal (VBA/Excel/XP)

1. ## Hex to Decimal (VBA/Excel/XP)

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

2. ## Re: Hex to Decimal (VBA/Excel/XP)

I noticed that VBA has Hex(), Hex\$(), Oct() and Oct\$() functions. Seems they forgot about going the other direction. <img src=/S/sad.gif border=0 alt=sad width=15 height=15>

3. ## Re: Hex to Decimal (VBA/Excel/XP)

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.

4. ## Re: Hex to Decimal (VBA/Excel/XP)

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

5. ## Re: Hex to Decimal (VBA/Excel/XP)

CDec() works as well:
<pre>?hex(1234)
4D2
?cdec(&H4D2)
1234
</pre>

(From the Immediate window)

6. ## Re: Hex to Decimal (VBA/Excel/XP)

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

7. ## Re: Hex to Decimal (VBA/Excel/XP)

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

#### Posting Permissions

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