Excel VBA Hex Function: Convert Numbers to Hex

VBA & macros · Updated November 21, 2026

In Excel VBA, Hex(number) converts a numeric value to a hexadecimal text string: for example, Hex(255) returns FF. It does not add a 0x prefix or pad the result with leading zeroes, so the output is text rather than a formatted number.

What the VBA Hex function returns

Hexadecimal is a base-16 number system. It uses the digits 0 through 9 and the letters A through F, where A represents 10 and F represents 15. Each hexadecimal digit corresponds to four binary bits, which makes hexadecimal a compact way to represent values used in programming, color codes, and data formats.

VBA’s Hex function takes a numeric expression and returns its hexadecimal representation as text. A few examples are:

  • Hex(10) returns A.
  • Hex(16) returns 10.
  • Hex(255) returns FF.
  • Hex(4096) returns 1000.

The result contains only the digits needed to express the value. Thus, 15 becomes F, not 0F. If a later operation requires a fixed number of characters, add the padding deliberately rather than assuming Hex supplies it.

Syntax and basic use in a macro

The syntax is Hex(number), where number is a numeric expression. In a VBA procedure, assign the returned string to a variable or use it when building a message or output value. This short example writes the hexadecimal form of 255 to the Immediate window:

Sub ShowHexExample()
    Dim decimalValue As Long
    Dim hexText As String

    ' Convert the number and display the text result.
    decimalValue = 255
    hexText = Hex$(decimalValue)
    Debug.Print hexText
End Sub

The Immediate window displays FF. In the VBA editor, open it with Ctrl+G on Windows. On Mac, use the VBA editor’s View > Immediate Window command if the window is not already visible. Hex$ is the string-returning form of the function; Hex is also commonly used and returns a Variant containing a string. Both are VBA functions, not ordinary worksheet functions.

Using hexadecimal values in worksheet cells

You cannot normally enter =Hex(A1) in a worksheet and expect Excel to call the VBA function. Hex belongs to VBA, so a worksheet formula reports an unrecognized function unless you create a VBA user-defined function, or UDF. A UDF can make the conversion available in cells:

Public Function ToHex(ByVal value As Long) As String
    ' Return the hexadecimal text for a whole-number value.
    ToHex = Hex$(value)
End Function

To add it in Excel for Microsoft 365 on Windows, press Alt+F11 to open the Visual Basic Editor, choose Insert > Module, and paste the function into the module. On Mac, use Option+F11 (some keyboards also require Fn) or open the Visual Basic Editor from the Developer tab. Save the workbook as an Excel Macro-Enabled Workbook (.xlsm) if you want to keep its VBA code.

With a whole number in cell A2, enter =ToHex(A2) in another cell; for example, if A2 contains 255, the result is FF. The UDF parameter above is a VBA Long, so the input must be a whole number in that type’s range. A decimal value or an out-of-range value needs validation or a different parameter strategy. Excel for the web can display a workbook containing macros, but it cannot create, edit, or run VBA macros.

Negative numbers, decimals, and output width

Negative numbers are represented using two’s complement, a binary encoding used to represent signed values. As a result, Hex may return a string of F characters and digits rather than a minus sign followed by the positive magnitude. The number of characters depends on the numeric type involved; for example, a 32-bit Long representation can use eight hexadecimal characters. Do not treat that result as a simple signed magnitude conversion.

If the value is fractional, VBA rounds it to a whole number before converting it. That can surprise someone who expects the decimal part to be discarded. Convert or validate the value explicitly when the distinction matters, and avoid relying on implicit rounding in data-cleaning code. If you need exactly two, four, or eight digits, define that requirement and pad the text yourself. For instance, a one-digit value can be displayed as two characters by adding a leading zero when its text length is one.

A fixed width matters when hexadecimal strings are identifiers, byte values, or fields in a file format. The text F and the text 0F represent the same numeric value, but they are not the same string. Before comparing, sorting, or exporting values, decide whether the task calls for numeric equality or character-for-character equality.

Hex versus hexadecimal literals and DEC2HEX

VBA also uses hexadecimal notation for numeric literals. A value written in code with the &H prefix is interpreted as a number; for example, &HFF is the numeric value 255. By contrast, Hex(255) produces the text FF. The prefix belongs to the notation for a literal and is not automatically included in the function’s result.

For a worksheet-only conversion, Excel has the DEC2HEX worksheet function. With a decimal number in A2, the formula =DEC2HEX(A2) returns its hexadecimal text; if A2 contains 255, it returns FF. The optional places argument can request leading zeroes, as in =DEC2HEX(A2,4), which returns 00FF for that input. These are worksheet formulas, not VBA statements. DEC2HEX and VBA Hex have different supported ranges and behavior for negative values, so check the relevant function’s limits before substituting one for the other.

Choose VBA Hex when code is already working with values in a macro, or use a UDF if the workbook specifically needs a custom VBA conversion in cells. Choose DEC2HEX when a standard worksheet formula is sufficient and you do not need to add VBA to the file. The worksheet option is often simpler to share because it does not require users to enable macros.

Common problems and how to avoid them

  • Expecting a prefix: The output is FF, not 0xFF or &HFF. Add the prefix as text only if the receiving system requires it.
  • Expecting leading zeroes: Hex returns a compact representation. Apply padding when a fixed-width string is part of the requirement.
  • Using it as a worksheet function: =Hex(A2) is not the VBA function. Use DEC2HEX in a cell or create a UDF such as ToHex.
  • Confusing text and numbers: The returned hexadecimal characters are text. They may not sort in the same order as the corresponding decimal values, and arithmetic requires conversion back to a number.
  • Ignoring the input range: VBA numeric types have limits. A value that does not fit the chosen type can cause an overflow or conversion error; check inputs before converting them.
  • Assuming an error is reversible: A macro that changes cells cannot be undone with Ctrl+Z. Save a copy of the workbook before running a macro, especially when adapting code that writes results into a range.

Hexadecimal and color values

Hexadecimal is often encountered when working with web colors, but the digits returned by VBA Hex are not automatically a web-style color string. A web color such as #RRGGBB places red, green, and blue components in that order. VBA’s RGB(red, green, blue) function returns a Long color value whose low-order byte represents red, followed by green and blue. Consequently, applying Hex directly to that Long can produce a byte order that looks reversed compared with the familiar web notation.

For example, a pure red VBA color value is numerically 255, whose hexadecimal representation is FF, not the six-character web string FF0000. A full web color string must be constructed by extracting each color component, converting each component separately, and padding each one to two digits. This is a useful distinction when transferring colors between Excel VBA, HTML, and other applications; do not assume that one hexadecimal conversion handles channel order and width for you.

Safety and workbook compatibility

The built-in Hex function is a simple conversion and does not alter a worksheet by itself. A macro that uses it can still change cells, files, or other workbook content, depending on the surrounding code. Review the complete procedure before running it, keep a backup, and enable macros only for workbooks you trust. A UDF used in a cell usually recalculates like other functions, but it still requires VBA support and an enabled macro environment.

On Windows, the Developer tab can be enabled through File > Options > Customize Ribbon. On Mac, use Excel > Preferences > Ribbon & Toolbar to show Developer if needed. The editor and VBA capabilities are desktop features; Excel for the web does not execute VBA, and a workbook relying on a custom UDF will not calculate that function there in the same way as desktop Excel. If recipients need a broadly compatible result, consider storing the converted values or using the worksheet’s built-in DEC2HEX function instead.

Frequently Asked Questions

Does Excel have a VBA Hex function?
Yes. In VBA, call Hex(number) or its string-returning form, Hex$(number). It converts a numeric expression to hexadecimal text. It is not directly available as a worksheet function under the name Hex.

How do I convert a decimal number to hex in an Excel cell?
Use the worksheet function DEC2HEX, such as =DEC2HEX(A2) when A2 contains the decimal value. If you need a VBA-specific conversion in a cell, place a UDF in a VBA module and call that function instead.

Why does Hex return letters instead of a number?
The function returns a string because hexadecimal digits include A through F and because the result is intended as a representation, not a decimal numeric value. Treat it as text when displaying, joining, or exporting it.

How can I make Hex return leading zeroes?
The VBA function does not provide a width argument. Add padding after conversion, using the number of characters required by your format. For worksheet conversions, DEC2HEX has an optional places argument for leading zeroes.

Can I use a VBA Hex UDF in Excel for the web?
No. Excel for the web does not run VBA. The workbook can be opened there, but a custom VBA function needs a desktop version of Excel to calculate. A standard worksheet function such as DEC2HEX is a better choice when web compatibility matters.

Related

Try it: the Column Letter to Number: aA is 27, XFD is 16,384. Converts both ways.

More in VBA & macros

Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources

Spotted an error in this guide? Let us know.