VBA HEX Function

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

What is VBA HEX Function

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.

how to use it

To use VBA's HEX function you need to understand its syntax and arguments:

Syntax

Hex(Number)

Arguments

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

Example to use HEX Function in VBA

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

Below are some important points which you need to take care while using HEX function in VBA.

  • 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

About the Author

puneet one point one

Puneet is using Excel since his college days. He helped thousands of people to understand the power of the spreadsheets and learn Microsoft Excel. You can find him online, tweeting about Excel, on a running track, or sometimes hiking up a mountain.