https://excelatfinance.com/xlf/xlf-colors-1.php
VBA colors
0. Quick guide to the RGB color model
In this module:
- RGB color circles
- ColorIndex property - 56 colours, and VBA ColorConstants
- RGB and decimal color values, plus conversion
- Color property
The RGB color model adds combinations of Red, Green, and Blue to produce various colours. Each component is an integer value 0 to 255. The RGB colors with intersection overlap is demonstrated in figure 1.
1. RGB colors
1.1 RGB circles with additive colour mixing
Three RGB circles with colour mixing - returns 7 colors.

With the inclusion of black (no colour), the eight colours are:
- Black: RGB(0,0,0)
- White: RGB(255,255,255)
- Red: RGB(255,0,0)
- Green: RGB(0,255,0)
- Blue: RGB(0,0,255)
- Yellow: RGB(255,255,0)
- Magenta: RGB(255,0,255)
- Cyan: RGB(0,255,255)
1.2 RGB circles - production code
Using web browser CSS and Scalable Vector Graphics (SVG)
Code for CSS style and Scalable Vector Graphics (SVG) used to produce the RGB circles in figure 1
| 1 2 3 4 5 6 7 8 9 10 | <styletype="text/css">    circle {mix-blend-mode: screen;}</style><svg>    <circlecx="75"cy="50"r="40"fill="rgb(255,0,0)"></circle>    <circlecx="50"cy="100"r="40"fill="rgb(0,255,0)"></circle>    <circlecx="100"cy="100"r="40"fill="rgb(0,0,255)"></circle></svg>    | 
2. VBA ColorIndex property
Syntax: expression.ColorIndex = value
where value is an element from the integer series 1,2, …, 56. Special values include xlColorIndexAutomatic (-4105) and xlColorIndexNone (-4142).
Examples of the ColorIndex property:
- xlRange.Value = "excel"
- xlRange.Interior.ColorIndex = 48
- xlRange.Font.ColorIndex = 20
- xlRange.Borders.ColorIndex = 3
- xlRange.Characters(1, 2).Font.ColorIndex = 6
2.1 The 56 colours of ColorIndex
Colours 2 to 8 are red, green, and blue with additive mixing. The colours 9 to 56 are various combinations of red, green and blue with RGB values: 0, 51, 102, 128, 150, 153, 192, 204, and 255 (figure 2).

The WS range in figure 2 was printed from the ColIndx2wWS procedure in code 1.
Code 1: Sub
ColIndx2WS procedure prints ColorIndex to WS (see figure 2)| 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | SubColIndx2WS()' Requires xlfDec2RGB(ColDec) functionDimi AsIntegerDimAC AsRange: SetAC = ActiveCell    Fori = 1 To56        AC(i, 1) = i        AC(i, 1).HorizontalAlignment = xlLeft        AC(i, 2).Interior.ColorIndex = i        AC(i, 3).Value = "RGB("& xlfDec2RGB(AC(i, 2).Interior.Color) & ")"' see code ...        AC(i, 3).HorizontalAlignment = xlRight    Nexti    AC(i, 3).ColumnWidth = 19EndSub | 
The colour table can also be printed as a 7 by 8 array (code 2)
Code 2: Sub
ColIndx2WSarraS procedure prints ColorIndex to WS array (7 rows by 8 columns)| 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 | SubColIndx2WSarray()ConstTLC AsString= "B2"' top left cellDimi AsInteger, j AsIntegerDimCol AsInteger, Val AsInteger    WithRange(TLC)    Fori = 1 To7        Forj = 1 To8            Col = i * j            Val = Val + 1            .Offset(i - 1, j - 1).Interior.ColorIndex = Col            .Offset(i - 1, j - 1).Value = Val            SelectCaseCol                Case1, 5, 9 To14, 16, 18, 21, 23, 25, 29, 30, 32, 41, 47 To49, 51 To56                    .Offset(i - 1, j - 1).Font.ColorIndex = 2                CaseElse                    .Offset(i - 1, j - 1).Font.ColorIndex = 1            EndSelect        Nextj    Nexti        .Resize(7, 8).ColumnWidth = 5        .Resize(7, 8).RowHeight = 20    EndWithEndSub | 
Ten of the ColorIndex colours are duplicate pairs: Blue 5 and 32 ; Yellow 6 and 27 ; Pink 7 and 26 ; Turquoise 8 and 28 ; Dark Red 9 and 30 ; Dark Blue 11 and 25 ; Violet 13 and 29 ; Teal 14 and 31 ; [No name] 18 and 54 ; and [No name] 20 and 34 . Leaving only 46 unique colours.
2.2 ColorConstants
The 8 colours listed in section 1.1 have name equivalents listed as members of the VBA ColorConstants class in the decimal colour system. The ColorConstants Auto List drop down is shown in figure 4.

Code 3 prints a list of the ColorConstants numerical values to the immediate window (figure 5).
Code 3: Sub
ColConst procedure| 60 61 62 63 64 65 66 67 68 69 70 71 72 | SubColConst()Dimi AsIntegerDimColArrVal AsVariantDimColArrLbl AsVariant    ColArrVal = Array(vbBlack, vbWhite, vbRed, vbGreen, vbBlue, vbYellow, vbMagenta, vbCyan)    ColArrLbl = Array("vbBlack", "vbWhite", "vbRed", "vbGreen", "vbBlue", "vbYellow", "vbMagenta", "vbCyan")    Fori = LBound(ColArrVal) ToUBound(ColArrVal)        Debug.PrintColArrLbl(i) & ": "& ColArrVal(i)    NextiEndSub | 

3. Decimal values of colours
3.1 RGB to positive decimal
To convert RGB to decimal , the relationship is . See code 4.
Code 4: Function
xlfRGB2DecX converts RGB values to decimal. Includes test routine| 75 76 77 78 79 80 81 82 83 84 85 86 87 88 | FunctionxlfRGB2DecX(Red AsInteger, Green AsInteger, Blue AsInteger) AsLong    xlfRGB2DecX = Red * 256 ^ 0 + Green * 256 ^ 1 + Blue * 256 ^ 2EndFunction' ===========================FunctionxlfRGB2DecY(Red AsInteger, Green AsInteger, Blue AsInteger) AsLong    xlfRGB2DecY = RGB(Red, Green, Blue)EndFunction' ===========================SubTestxlfRGB2DecX()DimAns AsLong    Ans = xlfRGB2DecX(204, 255, 255)EndSub | 
3.2 RGB to negative decimal
To convert RGB to Excel negative decimal, the relationship is
The color red is RGB(255,0,0), equal to 256 as a positive value. The Excel negative value equivalent is RGB - (256^3) - 1 = 256 - (256^3) - 1 = -16776961
3.3 Decimal to RGB
The backslash operator "\" divides two numbers and returns the integer quotient. The remainder is ignored.
Code 5: Function
xlfDec2RGB converts color decimal to RGB comma separated values. Includes test routine| 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 | FunctionxlfDec2RGB(ByValColDec AsLong) AsStringDimR AsLongDimG AsLongDimb AsLong  R = (ColDec And&HFF) \ 256 ^ 0      ' &HFF hexadecimal = 255 decimal                                       ' leading &H is the prefix radix (base) for hexadecimal  G = (ColDec And&HFF00&) \ 256 ^ 1   ' &HFF00& hexadecimal = 65280 decimal                                       ' trailing & is a TDC for type long, if                                       ' omitted (&HFF00), the assigned value is -256  b = ColDec \ 256 ^ 2  xlfDec2RGB = CStr(R) & ","& CStr(G) & ","& CStr(b)EndFunction' ===========================SubTestxlfDec2RGB()DimAns AsString    Ans = xlfDec2RGB(16737843)  ' returns 51,102,255    StopEndSub | 
About code 5
- Line 95: 16737843 And 255returns 51,51 \ 1returns 51, remainder 0
 Further details of the binary And are provided in figure 5. Only the last 8 digits on the right are relevant.- 16737843 = 111111110110011000110011;
- 255 = 11111111;
- AND returns 00110011 = 51
 
- 16737843 = 111111110110011000110011;
 - Fig 5: binary AND - last 8 digits on the right 
- Line 97: 16737843 And 65280returns 26112,26112 \ 256returns 102, remainder 0
- Line 100: 16737843 \ 65536returns 255, remainder 2616
4. Color property
Syntax: expression.Color = value
where value can be created by the RGB function returned as a long whole number.
Examples of the Color property:
- xlRange.Value = "excel"
- xlRange.Interior.Color = RGB(150,150,150)(equivalent to ColorIndex 48)
- xlRange.Font.Color = 16777164(equivalent to: RGB(204,255,255); ColorIndex 48)
- xlRange.Borders.Color = RGB(150,0,0)(equivalent to ColorIndex 3)
- xlRange.Characters(1, 2).Font.Color = 6(equivalent to: RGB(6,0,0))
- RGB discussion [23 Apr 2018]
- Development platform: Excel 2016 64 bit.
- Published: 14th April 2016
- Revised: Monday 28th of January 2019 - 10:28 PM, Pacific Time (PT)
