How to use the VBA HEX Function (Syntax + Example)

HomeVBA Functions LIST (Category Wise)How to use the VBA HEX Function (Syntax + Example)

The VBA HEX function is listed under the data type conversion category of VBA functions. When you use it in a VBA code, it converts an expression into hexadecimal notation and returns the result as a string. Hexadecimal notation is just another base for representing numbers in, like binary. You can learn more about it from here.

Syntax

Hex(Number)

Arguments

  • Number: The numeric value that you want to convert into hexadecimal.

Example

To practically understand how to use VBA HEX function, you need to go through the below example where we have written a vba code by using it:

Sub example_HEX()
Range("B1").Value = Hex(Range("A1"))
End Sub

In the above code, we have used HEX to convert the number from the cell A1 into a hexadecimal notation and return the result as a string in the cell B1.

Notes

  • If the value specified is a value other than a number or a string that can’t be recognized as a number, VBA will return the run-time 13 error.
  • If the given number is not a whole number, then it is rounded to the nearest whole number.
  • If the supplied number is NULL then it will return NULL in the result.

Related Functions

CBOOL | CBYTE | CCUR | CDATE | CDBL | CDEC | CHR | CINT | CLNG | CSNG | CSTR | CVAR | FORMATCURRENCY | FORMATDATETIME | FORMATNUMBER | FORMATPERCENT | OCT | STR | VAL