Excel VBA Hex Function

VBA Hex Function: The Hex function in VBA converts the given number into hexadecimal notation and returns the result as a string.

Syntax

Hex (Number)

Parameter

Number (required) – This parameter represents the numeric value that you want to convert to hexadecimal.

Return

This function returns a string value after converting the specified number into hexadecimal notation. If the number is a decimal, it will round it to the nearest integer before converting it to hexadecimal.

Example 1

Sub HexFunction_Example1()
 ' Converting decimal values into hexadecimal notation.
 Dim hex_val As String
 hex_val = Hex(15)
 ' The variable hex_val will return the String "F".
 Cells(1, 1).Value = hex_val
 End Sub 

Output

F

VBA Hex Function

Example 2

Sub HexFunction_Example2()
 ' Converting decimal values into hexadecimal notation.
 Dim hex_val As String
 hex_val = Hex(2000)
 ' The variable hex_val will return the String "7D0".
 Cells(1, 1).Value = hex_val
 End Sub 

Output

7D0

VBA Hex Function

Example 3

Sub HexFunction_Example3()
 ' Converting octal into hexadecimal notation.
 Dim hex_val As String
 hex_val = Hex(&O2751)
 ' The variable hex_val will return the String "5.00E+09".
 Cells(1, 1).Value = hex_val
 End Sub 

Output

5.00E+09

VBA Hex Function