VBA HEX Function (Syntax + Example)

Last Updated: June 22, 2023
puneet-gogia-excel-champs

- Written by Puneet

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 here.

Syntax

Hex(Number)

Arguments

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

Example

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

example-vba-hex-function
Sub example_HEX()
Range("B1").Value = Hex(Range("A1"))
End Sub

In the above code, we have used HEX to convert the number from cell A1 into a hexadecimal notation and return the result as a string in 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.