Excel VBA Hex Conversion: How to Convert Between Hex and Decimal
In this article
Excel VBA hex conversion lets you turn hexadecimal values into decimal numbers and vice versa using built-in functions like Hex() and CLng(&H...). This is essential for working with color codes, memory addresses, or low-level data in automation scripts. No external tools or add-ins are needed—just VBA in Excel. The conversions happen instantly in code, making it ideal for batch processing data from sensors, APIs, or hardware interfaces.
What You'll Need
- Microsoft Excel with VBA enabled (Excel 2010 or later)
- Basic familiarity with the VBA Editor (Alt+F11)
- A worksheet with hex values to convert, or a need to generate hex from decimal numbers
Step-by-Step
- Open Excel and press Alt+F11 to launch the VBA Editor.
- In the VBA Editor, go to Insert > Module to create a new blank module.
- Copy and paste the following function to convert a decimal number to hexadecimal:
Function DecToHex(decimalValue As Long) As String
DecToHex = Hex(decimalValue)
End Function
- Below it, paste this function to convert a hexadecimal string to decimal:
Function HexToDec(hexValue As String) As Long
HexToDec = CLng("&H" & hexValue)
End Function
- Close the VBA Editor and return to your Excel worksheet.
- In any cell, enter =DecToHex(255) to convert decimal 255 to hex. The result will be FF.
- In another cell, enter =HexToDec("FF") to convert hex FF back to decimal. The result will be 255.
- For multi-digit hex values, ensure they are passed as text. Use =HexToDec("1A3F") to convert 1A3F to 6719.
- To apply these functions to a range, drag the formula down or use a loop in VBA. For example, to convert column A (decimal) to column B (hex), use:
Sub ConvertColumnToHex()
Dim i As Long
For i = 1 To Range("A" & Rows.Count).End(xlUp).Row
Cells(i, 2).Value = DecToHex(Cells(i, 1).Value)
Next i
End Sub
- Press F5 while in the VBA Editor to run the macro, or assign it to a button on the worksheet via Developer > Insert > Button.
- Test with edge cases: =DecToHex(0) returns "0", and =HexToDec("0") returns 0.
Common Mistakes
- Forgetting the &H prefix when using CLng(). Using CLng("FF") instead of CLng("&HFF") will cause a Type Mismatch error.
- Passing hex strings with leading or trailing spaces. Use Trim(hexValue) before conversion: CLng("&H" & Trim(hexValue)).
- Using Hex() on non-integer values. Hex() only accepts Long integers. Passing 255.7 will truncate to 255, which may be unintended.
- Assuming hex strings are case-sensitive. Hex() returns uppercase, but CLng() accepts both uppercase and lowercase ("ff" and "FF" both work).
- Trying to convert hex strings longer than 8 digits without using a data type that supports larger numbers. CLng() supports up to 2,147,483,647. For larger values, use Currency or Double with custom parsing.
Tips
- Use Hex() with zero-padding for consistent output: Format(Hex(15), "00") returns "0F" instead of "F".
- To convert RGB colors to hex, use: Hex(RGB(255,0,0)) returns "FF0000" for red.
- For batch processing, store hex values as text in Excel cells to prevent auto-formatting (e.g., Excel turning "1E5" into a date).
- Use Application.WorksheetFunction.Hex2Dec() in Excel 2013 and later for worksheet-level conversion without VBA. But this function doesn't exist in older versions.
- When debugging, print intermediate values using Debug.Print Hex(255) in the Immediate Window (Ctrl+G in VBA Editor).
- Combine with Replace() to remove unwanted prefixes: Replace(Hex(255), "&H", "") is unnecessary since Hex() never returns the prefix.
Troubleshooting
- If you get a "Compile error: Expected: End If" or similar, check for missing End Function or mismatched quotes in your code.
- If =HexToDec("FF") returns #NAME?, ensure the VBA function is saved in a standard module (not a worksheet or ThisWorkbook module) and that Excel is set to enable macros (File > Options > Trust Center > Macro Settings > Enable all macros temporarily for testing).
- If the result is 0 when converting a hex string, check if the string contains invalid characters like "G", "H", or symbols. Only 0-9 and A-F are valid.
- If CLng() throws an overflow error, the hex string represents a number larger than 2,147,483,647. Use Double or split the string into parts for larger conversions.
- If the macro runs but does nothing, confirm the range in your loop is correct. Use Range("A1").CurrentRegion to select contiguous data.
- If Excel crashes or freezes during macro execution, break the loop into smaller chunks or add DoEvents after each iteration for responsiveness.
Frequently Asked Questions
Can I convert hex to decimal without VBA in Excel?
Yes, if you’re using Excel 2013 or later, use the built-in function HEX2DEC(). For example, =HEX2DEC("FF") returns 255. Older versions require VBA or manual lookup tables.
Why does Hex(255) return "FF" but Hex(-1) return "FFFFFFFF"?
Hex() treats negative numbers as two's complement. -1 in 32-bit signed integer is represented as FFFFFFFF in hex. This is correct behavior for low-level systems.
Can I convert hex fractions like "A.8" in VBA?
No. VBA’s Hex() and CLng() only work with whole numbers. You must parse fractional hex manually by splitting the integer and fractional parts and converting each separately.
What if my hex string has a "0x" prefix like "0xFF"?
Remove it first: HexToDec(Replace("0xFF", "0x", "")) works. Or use: HexToDec(Right(hexValue, Len(hexValue) - 2)) if the prefix is always two characters.
Does this work with Unicode or UTF-8 hex values?
No. Hex conversion here is for numeric values only. To decode UTF-8 hex bytes, you need string parsing and binary-to-character translation, which requires additional code beyond Hex() and CLng().
How do I handle empty cells in my conversion loop?
Add a check: If Not IsEmpty(Cells(i, 1)) Then [... End If. Otherwise, Hex() will throw a Type Mismatch error on blank cells.