|&H10||=||Hexadecimal 10 (=decimal 16)|
|&O10||=||Octal 10 (=decimal 8)|
The VBA Hex function converts a supplied number into hexadecimal notation and returns the result as a string.
The syntax of the function is:
Note that, if the supplied Number is a decimal, it is rounded to the nearest integer before being converted to hexadecimal.
' Convert three different decimals into hexadecimal notation.
Dim hex1 As Stringhex3 = Hex( 1000 )
Dim hex2 As String
Dim hex3 As String
' hex3 is now equal to the String "3E8".
After running the above vba code the variables hex1, hex2 and hex3 are equal to the Strings "F", "10" and "3E8" respectively.
The VBA Hex function can also be used to convert octal values to hexadecimal, if the octal is preceded by &O.
' Convert the octal 1750 into hexadecimal notation.Dim hex1 As String
hex1 = Hex( &O1750 )
' hex1 is now equal to the String "3E8".
Warning: Text representations of hexadecimals or octals (as returned by the Hex and Oct functions) are not recognised as hexadecimals or octals when supplied to VBA functions.
If the Hex function is supplied with a text string that cannot be interpreted as a numeric value, it will return the error: