VBA HEX Function (Syntax + Example)

Last Updated: June 22, 2023

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




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


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:

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.


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