HEX to VBA Color Codes
Turn a HEX, RGB or HSL color into the color value VBA uses in Excel, Access and Word: the RGB() call, the Long number and the &H literal. Or paste a value from VBA code to see which color it is.
Color to VBA
RGB() call
RGB(255, 107, 53)Color value (Long)
3501055Hex literal (note the BGR order)
&H00356BFF&Closest ColorIndex (default Excel palette)
46#FF6600, ΔE 5.4 (Approximate). For the exact color use .Color = RGB(255, 107, 53).
Excel
Range("A1").Interior.Color = RGB(255, 107, 53)
Range("A1").Font.Color = 3501055Access
Me.txtName.BackColor = RGB(255, 107, 53) Me.txtName.ForeColor = &H00356BFF&
VBA to color
HEX
#FF6B35CSS rgb()
rgb(255, 107, 53)Color value (Long)
3501055VBA functions to convert HEX in your own code
' HEX "#FF6B35" -> VBA color (Long), for .Color, .BackColor, .ForeColor
Function HexToVbaColor(ByVal hexColor As String) As Long
hexColor = Replace(Trim(hexColor), "#", "")
If Len(hexColor) <> 6 Then Err.Raise 5, , "Use a 6-digit HEX such as #FF6B35"
HexToVbaColor = RGB(CLng("&H" & Mid(hexColor, 1, 2)), _
CLng("&H" & Mid(hexColor, 3, 2)), _
CLng("&H" & Mid(hexColor, 5, 2)))
End Function
' VBA color (Long) -> HEX "#FF6B35"
Function VbaColorToHex(ByVal colorValue As Long) As String
VbaColorToHex = "#" & Right("0" & Hex(colorValue And &HFF&), 2) & _
Right("0" & Hex((colorValue \ &H100&) And &HFF&), 2) & _
Right("0" & Hex((colorValue \ &H10000) And &HFF&), 2)
End Function
' Example: Range("A1").Interior.Color = HexToVbaColor("#FF6B35")Paste into a standard module (Alt+F11, then Insert → Module). HexToVbaColor builds the value with RGB(), so red and blue end up in the byte order VBA expects.
| ColorIndex | HEX | RGB() | Long |
|---|---|---|---|
| 1 | #000000 | RGB(0, 0, 0) | 0 |
| 2 | #FFFFFF | RGB(255, 255, 255) | 16777215 |
| 3 | #FF0000 | RGB(255, 0, 0) | 255 |
| 4 | #00FF00 | RGB(0, 255, 0) | 65280 |
| 5 | #0000FF | RGB(0, 0, 255) | 16711680 |
| 6 | #FFFF00 | RGB(255, 255, 0) | 65535 |
| 7 | #FF00FF | RGB(255, 0, 255) | 16711935 |
| 8 | #00FFFF | RGB(0, 255, 255) | 16776960 |
| 9 | #800000 | RGB(128, 0, 0) | 128 |
| 10 | #008000 | RGB(0, 128, 0) | 32768 |
| 11 | #000080 | RGB(0, 0, 128) | 8388608 |
| 12 | #808000 | RGB(128, 128, 0) | 32896 |
| 13 | #800080 | RGB(128, 0, 128) | 8388736 |
| 14 | #008080 | RGB(0, 128, 128) | 8421376 |
| 15 | #C0C0C0 | RGB(192, 192, 192) | 12632256 |
| 16 | #808080 | RGB(128, 128, 128) | 8421504 |
| 17 | #9999FF | RGB(153, 153, 255) | 16751001 |
| 18 | #993366 | RGB(153, 51, 102) | 6697881 |
| 19 | #FFFFCC | RGB(255, 255, 204) | 13434879 |
| 20 | #CCFFFF | RGB(204, 255, 255) | 16777164 |
| 21 | #660066 | RGB(102, 0, 102) | 6684774 |
| 22 | #FF8080 | RGB(255, 128, 128) | 8421631 |
| 23 | #0066CC | RGB(0, 102, 204) | 13395456 |
| 24 | #CCCCFF | RGB(204, 204, 255) | 16764108 |
| 25 | #000080 | RGB(0, 0, 128) | 8388608 |
| 26 | #FF00FF | RGB(255, 0, 255) | 16711935 |
| 27 | #FFFF00 | RGB(255, 255, 0) | 65535 |
| 28 | #00FFFF | RGB(0, 255, 255) | 16776960 |
| 29 | #800080 | RGB(128, 0, 128) | 8388736 |
| 30 | #800000 | RGB(128, 0, 0) | 128 |
| 31 | #008080 | RGB(0, 128, 128) | 8421376 |
| 32 | #0000FF | RGB(0, 0, 255) | 16711680 |
| 33 | #00CCFF | RGB(0, 204, 255) | 16763904 |
| 34 | #CCFFFF | RGB(204, 255, 255) | 16777164 |
| 35 | #CCFFCC | RGB(204, 255, 204) | 13434828 |
| 36 | #FFFF99 | RGB(255, 255, 153) | 10092543 |
| 37 | #99CCFF | RGB(153, 204, 255) | 16764057 |
| 38 | #FF99CC | RGB(255, 153, 204) | 13408767 |
| 39 | #CC99FF | RGB(204, 153, 255) | 16751052 |
| 40 | #FFCC99 | RGB(255, 204, 153) | 10079487 |
| 41 | #3366FF | RGB(51, 102, 255) | 16737843 |
| 42 | #33CCCC | RGB(51, 204, 204) | 13421619 |
| 43 | #99CC00 | RGB(153, 204, 0) | 52377 |
| 44 | #FFCC00 | RGB(255, 204, 0) | 52479 |
| 45 | #FF9900 | RGB(255, 153, 0) | 39423 |
| 46 | #FF6600 | RGB(255, 102, 0) | 26367 |
| 47 | #666699 | RGB(102, 102, 153) | 10053222 |
| 48 | #969696 | RGB(150, 150, 150) | 9868950 |
| 49 | #003366 | RGB(0, 51, 102) | 6697728 |
| 50 | #339966 | RGB(51, 153, 102) | 6723891 |
| 51 | #003300 | RGB(0, 51, 0) | 13056 |
| 52 | #333300 | RGB(51, 51, 0) | 13107 |
| 53 | #993300 | RGB(153, 51, 0) | 13209 |
| 54 | #993366 | RGB(153, 51, 102) | 6697881 |
| 55 | #333399 | RGB(51, 51, 153) | 10040115 |
| 56 | #333333 | RGB(51, 51, 51) | 3355443 |
How VBA stores a color
A color in VBA is a whole number of type Long. The RGB() function builds it as red + green × 256 + blue × 65536, so each channel takes one byte, red in the lowest. That makes the hex form read in reverse: blue, green, red. Pure red is 255 (&HFF&), pure blue is 16711680 (&HFF0000&). The eight built-in constants, from vbBlack to vbWhite, are the same numbers with names.
Values from &H80000000 upward, which VBA shows as negative numbers, are not colors but references to Windows system colors such as the window background or button face. The converter names them instead of guessing a color. The formula, the constants and the system color list follow Microsoft’s VBA language reference.
The ColorIndex table is the default palette of an Excel workbook. Its values are the indexed colors 8 to 63 of the Office Open XML standard (ECMA-376), and they match the default Excel palette in LibreOffice’s file import, all 56 of them. The closest index is found by CIEDE2000 ΔE.
Other color tools
For every screen format at once see the color converter; for a single format, HEX to RGB or RGB to HEX.
VBA colors: questions
- How do I convert a HEX color to VBA?
- Split the HEX code into red, green and blue and pass them to RGB(): #FF6B35 becomes RGB(255, 107, 53). RGB() returns a Long equal to red + green × 256 + blue × 65536, here 3501055, which you can also assign to a Color property directly.
- Why is the VBA hex value reversed?
- VBA stores a color with red in the lowest byte and blue in the highest, so written in hex it reads blue, green, red: #FF6B35 is &H00356BFF& in VBA. Copying a web HEX code into an &H literal unchanged swaps red and blue.
- Why does &HFF00 give the wrong color?
- A hex literal of up to four digits without a trailing & is an Integer in VBA, so &HFF00 equals −256 instead of 65280 (green). Add the type suffix: &HFF00& is the Long 65280. This page writes every literal as &H00BBGGRR& for that reason.
- What does a negative color value such as −2147483643 mean?
- It is a Windows system color, not an RGB color. Values from &H80000000 up refer to the colors of the Windows theme; −2147483643 is &H80000005, vbWindowBackground. The actual color depends on each computer’s settings, so it cannot be turned into a fixed HEX code.
- What are the Excel ColorIndex values?
- ColorIndex is a position from 1 to 56 in the workbook palette, not a color value. In a workbook with the default palette, 1 is black, 2 white, 3 red, 4 green, 5 blue and 6 yellow; the full table is on this page, and the converter shows the closest index for any color. Some colors appear twice: yellow is both 6 and 27. If a workbook changed its palette, the same numbers give other colors, so for an exact color set .Color to an RGB() value instead.