RGB Colours
RGB Colours
VBA colors
In this module:
4. 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
Fig 1: - three circles; Red, Green, and Blue (RGB), with 4 intersection points
With the inclusion of black (no colour), the eight colours are:
1. Black: RGB(0,0,0)
2. White: RGB(255,255,255)
3. Red: RGB(255,0,0)
4. Green: RGB(0,255,0)
5. Blue: RGB(0,0,255)
6. Yellow: RGB(255,255,0)
7. Magenta: RGB(255,0,255)
8. Cyan: RGB(0,255,255)
Code for CSS style and Scalable Vector Graphics (SVG) used to produce the RGB circles in figure 1
https://excelatfinance.com/xlf/xlf-colors-1.php 1/7
2/1/25, 12:21 PM RGB colours
1 <style type="text/css">
2 circle {mix-blend-mode: screen;}
3 </style>
4
5 <svg>
6 <circle cx="75" cy="50" r="40" fill="rgb(255,0,0)"></circle>
7 <circle cx="50" cy="100" r="40" fill="rgb(0,255,0)"></circle>
8 <circle cx="100" cy="100" r="40" fill="rgb(0,0,255)"></circle>
9 </svg>
10
where value is an element from the integer series 1,2, …, 56. Special values include xlColorIndexAutomatic (-4105) and xlColorIndexNone (-4142).
xlRange.Value = "excel"
xlRange.Interior.ColorIndex = 48
xlRange.Font.ColorIndex = 20
xlRange.Borders.ColorIndex = 3
xlRange.Characters(1, 2).Font.ColorIndex = 6
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).
https://excelatfinance.com/xlf/xlf-colors-1.php 2/7
2/1/25, 12:21 PM RGB colours
The WS range in figure 2 was printed from the ColIndx2wWS procedure in code 1.
15 Sub ColIndx2WS()
16 ' Requires xlfDec2RGB(ColDec) function
17 Dim i As Integer
18 Dim AC As Range: Set AC = ActiveCell
19
20 For i = 1 To 56
21 AC(i, 1) = i
22 AC(i, 1).HorizontalAlignment = xlLeft
23 AC(i, 2).Interior.ColorIndex = i
24 AC(i, 3).Value = "RGB(" & xlfDec2RGB(AC(i, 2).Interior.Color) & ")" ' see code ...
25 AC(i, 3).HorizontalAlignment = xlRight
26 Next i
27 AC(i, 3).ColumnWidth = 19
28 End Sub
https://excelatfinance.com/xlf/xlf-colors-1.php 3/7
2/1/25, 12:21 PM RGB colours
30 Sub ColIndx2WSarray()
31 Const TLC As String = "B2" ' top left cell
32 Dim i As Integer, j As Integer
33 Dim Col As Integer, Val As Integer
34
35 With Range(TLC)
36 For i = 1 To 7
37 For j = 1 To 8
38 Col = i * j
39 Val = Val + 1
40 .Offset(i - 1, j - 1).Interior.ColorIndex = Col
41 .Offset(i - 1, j - 1).Value = Val
42 Select Case Col
43 Case 1, 5, 9 To 14, 16, 18, 21, 23, 25, 29, 30, 32, 41, 47 To 49, 51 To 56
44 .Offset(i - 1, j - 1).Font.ColorIndex = 2
45 Case Else
46 .Offset(i - 1, j - 1).Font.ColorIndex = 1
47 End Select
48 Next j
49 Next i
50 .Resize(7, 8).ColumnWidth = 5
51 .Resize(7, 8).RowHeight = 20
52 End With
53
54 End Sub
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.
Fig 4: VBA ColorConstants - VBA Auto List drop down with 8 items
https://excelatfinance.com/xlf/xlf-colors-1.php 4/7
2/1/25, 12:21 PM RGB colours
Code 3 prints a list of the ColorConstants numerical values to the immediate window (figure 5).
60 Sub ColConst()
61 Dim i As Integer
62 Dim ColArrVal As Variant
63 Dim ColArrLbl As Variant
64
65 ColArrVal = Array(vbBlack, vbWhite, vbRed, vbGreen, vbBlue, vbYellow, vbMagenta, vbCyan)
66 ColArrLbl = Array("vbBlack", "vbWhite", "vbRed", "vbGreen", "vbBlue", "vbYellow", "vbMagenta", "vbCyan")
67
68 For i = LBound(ColArrVal) To UBound(ColArrVal)
69 Debug.Print ColArrLbl(i) & ": " & ColArrVal(i)
70 Next i
71
72 End Sub
Code 4: Function xlfRGB2DecX converts RGB values to decimal. Includes test routine
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
https://excelatfinance.com/xlf/xlf-colors-1.php 5/7
2/1/25, 12:21 PM RGB colours
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
About code 5
Line 95: 16737843 And 255 returns 51, 51 \ 1 returns 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
Line 97: 16737843 And 65280 returns 26112, 26112 \ 256 returns 102, remainder 0
Line 100: 16737843 \ 65536 returns 255, remainder 2616
4. Color property
where value can be created by the RGB function returned as a long whole number.
xlRange.Value = "excel"
xlRange.Interior.Color = RGB(150,150,150) (equivalent to ColorIndex 48)
https://excelatfinance.com/xlf/xlf-colors-1.php 6/7
2/1/25, 12:21 PM RGB colours
Development platform: Excel 2016 64 bit.
Published: 14th April 2016
Revised: Friday 24th of February 2023 - 02:37 PM, Pacific Time (PT)
https://excelatfinance.com/xlf/xlf-colors-1.php 7/7