In this Article
- VBA Color Index Codes List
- VBA ColorIndex Examples
- VBA Color Property
- VB Color
- RGB Colors
- ColorIndex Codes List & RGB Colors in Access VBA
VBA Color Index Codes List
When using VBA to code the Colorindex (or background color) of a cell it is useful to know what integer will equal what color. Below is a reference picture which shows the color and lists it’s respective Colorindex. aka VBA Color Palette
Here’s the code to make one for yourself, or just bookmark this page:
Sub ColorRef()
Dim x As Integer
For x = 1 To 56
If x < Then
Cells(x, 1).Interior.ColorIndex = x
Cells(x, 2) = x
Else
Cells(x - 28, 3).Interior.ColorIndex = x
Cells(x - 28, 4) = x
End If
Next x
End Sub
VBA ColorIndex Examples
Set Cell Background Color
This example sets the cell’s background color.
Range("A1").Interior.ColorIndex = 6
Set Cell Font Color
This example sets the cell’s font color.
Range("A1").Font.ColorIndex = 5
Set Cell Borders Color
This example sets the cell’s border color.
Range("A1").Borders.ColorIndex = 5
Get Cell Background ColorIndex
This example gets the cell’s background color and assigns it to an Integer variable.
Dim col as Integer
col = Range("A1").Interior.ColorIndex
Set a Cell Background Color to Another Cell’s Color
This example sets a cell color equal to another cell color.
Range("A1").Interior.ColorIndex = Range("B1").Interior.ColorIndex
VBA Color Property
Instead of using Excel / VBA’s ColorIndex property, you can use the Color property. The Color property takes two input types:
- vbColor
- RGB Colors
We will discuss these below:
VB Color
VB Color is the easiest way to set colors in VBA. However, it’s also the least flexible. To set a color code using vbColor use the table below:
However, as you can see from the table, your options are extremely limited.
Set Cell Background Color
Range("A1").Interior.Color = vbYellow
Set Cell Font Color
Range("A1").Font.Color = vbBlue
Set Cell Borders Color
Range("A1").Borders.Color = vbRed
Set a Cell Background Color to Another Cell’s Color
Range("A1").Interior.Color = Range("B1").Interior.Color
RGB Colors
RGB stands for Red Green Blue. These are the three primary colors that can be combined to produce any other color. When entering colors as RGB, enter a value between 0 and 255 for each color code.
Here’s an example:
Range("A1").Interior.Color = RGB(255,255,0)
Above we’ve set Red = 255 (max value), Green = 255 (max value), and Blue = 0 (min value). This sets the cell background color to Yellow.
Instead we can set the cell font color to purple:
Range("A1").Interior.Color = RGB(128,0,128)
There are numerous online tools to find the RGB code for your desired color (here’s one).
ColorIndex Codes List & RGB Colors in Access VBA
Access uses forms to display data. You can use the ColorIndex Codes to programmatically change the background color and foreground color of objects in your Access forms.
Private Sub cmdSave_Click()
'change the background color of the save button when the record is saved.
DoCmd.RunCommand acCmdSaveRecord
cmdSave.BackColor = vbGreen
End Sub
Наименования цветов стандартной палитры Excel из 56 оттенков на русском и английском языках. Соответствие цветов индексам VBA и их RGB и HEX коды.
Стандартная палитра из 56 цветов — это ограниченная палитра оттенков, которая существовала до версии Excel 2007 года, но продолжает использоваться до сих пор.
За ограниченную палитру в VBA Excel отвечает свойство ColorIndex, которое используется для записи и чтения:
Sub Primer() Range(«A1»).Interior.ColorIndex = 10 MsgBox Range(«A1»).Interior.ColorIndex End Sub |
Свойство ColorIndex принимает и возвращает значения от 1 до 56.
Наименования 56 цветов палитры
Наименования 56 цветов стандартной ограниченной палитры Excel на русском и английском языках:
Индекс | Наименование по-русски |
Наименование по-английски | Оттенок |
---|---|---|---|
1 | Черный | Black | |
2 | Белый | White | |
3 | Красный | Red | |
4 | Лайм | Lime | |
5 | Синий | Blue | |
6 | Желтый | Yellow | |
7 | Фуксия* | Fuchsia* | |
8 | Цвет морской волны | Aqua | |
9 | Темно-бордовый | Maroon | |
10 | Зеленый | Green | |
11 | Темно-синий | Navy blue | |
12 | Оливковый | Olive | |
13 | Пурпурный | Purple | |
14 | Бирюзовый | Teal | |
15 | Серебряный | Silver | |
16 | Серый | Gray | |
17 | Светло-пурпурно-синий | Light purple blue | |
18 | Розовато-лиловый | Mauve | |
19 | Бледно-желто-зеленый | Pale yellow green | |
20 | Бледно-голубой | Pale blue | |
21 | Сливовый | Plum | |
22 | Лососевый | Salmon | |
23 | Темно-сине-голубой | Light navy blue | |
24 | Барвинок | Periwinkle | |
25 | Темно-синий | Navy blue | |
26 | Фуксия* | Fuchsia* | |
27 | Желтый | Yellow | |
28 | Цвет морской волны | Aqua | |
29 | Пурпурный | Purple | |
30 | Коричнево-малиновый | Maroon | |
31 | Сине-зеленый | Teal | |
32 | Синий | Blue | |
33 | Небесно-голубой | Vivid sky blue | |
34 | Бледно-голубой | Pale blue | |
35 | Бледно-зеленый | Pale green | |
36 | Светло-желтый | Light yellow | |
37 | Сине-голубой | Shade of blue | |
38 | Бледно-пурпурно-розовый | Pale magenta pink | |
39 | Светло-сиреневый | Light lilac | |
40 | Оранжево-персиковый | Peach-orange | |
41 | Светло-синий | Light blue | |
42 | Светло-бирюзовый | Light turquoise | |
43 | Желто-зеленый | Yellow green | |
44 | Желтый мандарин | Tangerine yellow | |
45 | Сигнальный оранжевый | Safety orange | |
46 | Оранжевый | Orange | |
47 | Темно-сине-серый | Dark blue gray | |
48 | Светло-серый | Light gray | |
49 | Полуночно-синий | Midnight blue | |
50 | Зеленая трава | Green grass | |
51 | Темно-зеленый | Dark green | |
52 | Темно-коричневый | Dark brown | |
53 | Темно-красный | Dark red | |
54 | Розовато-лиловый | Mauve | |
55 | Синий пигмент | Blue pigment | |
56 | Темный уголь | Dark charcoal |
* Вместо термина Фуксия (Fuchsia) можно использовать термин Маджента (Magenta), так как в RGB-модели Фуксия и Маджента являются одним и тем же цветом.
Обратите внимание, что в стандартной палитре Excel из 56 цветов есть повторяющиеся оттенки. Наименования цветов (по их HEX-кодам) подобраны, главным образом, по статьям, опубликованным в Википедии.
RGB и HEX коды оттенков палитры
RGB и HEX коды оттенков 56-цветной палитры Excel:
Индекс | Код HEX | Код RGB | Оттенок |
---|---|---|---|
1 | #000000 | RGB(0, 0, 0) | |
2 | #ffffff | RGB(255, 255, 255) | |
3 | #ff0000 | RGB(255, 0, 0) | |
4 | #00ff00 | RGB(0, 255, 0) | |
5 | #0000ff | RGB(0, 0, 255) | |
6 | #ffff00 | RGB(255, 255, 0) | |
7 | #ff00ff | RGB(255, 0, 255) | |
8 | #00ffff | RGB(0, 255, 255) | |
9 | #800000 | RGB(128, 0, 0) | |
10 | #008000 | RGB(0, 128, 0) | |
11 | #000080 | RGB(0, 0, 128) | |
12 | #808000 | RGB(128, 128, 0) | |
13 | #800080 | RGB(128, 0, 128) | |
14 | #008080 | RGB(0, 128, 128) | |
15 | #c0c0c0 | RGB(192, 192, 192) | |
16 | #808080 | RGB(128, 128, 128) | |
17 | #9999ff | RGB(153, 153, 255) | |
18 | #993366 | RGB(153, 51, 102) | |
19 | #ffffcc | RGB(255, 255, 204) | |
20 | #ccffff | RGB(204, 255, 255) | |
21 | #660066 | RGB(102, 0, 102) | |
22 | #ff8080 | RGB(255, 128, 128) | |
23 | #0066cc | RGB(0, 102, 204) | |
24 | #ccccff | RGB(204, 204, 255) | |
25 | #000080 | RGB(0, 0, 128) | |
26 | #ff00ff | RGB(255, 0, 255) | |
27 | #ffff00 | RGB(255, 255, 0) | |
28 | #00ffff | RGB(0, 255, 255) | |
29 | #800080 | RGB(128, 0, 128) | |
30 | #800000 | RGB(128, 0, 0) | |
31 | #008080 | RGB(0, 128, 128) | |
32 | #0000ff | RGB(0, 0, 255) | |
33 | #00ccff | RGB(0, 204, 255) | |
34 | #ccffff | RGB(204, 255, 255) | |
35 | #ccffcc | RGB(204, 255, 204) | |
36 | #ffff99 | RGB(255, 255, 153) | |
37 | #99ccff | RGB(153, 204, 255) | |
38 | #ff99cc | RGB(255, 153, 204) | |
39 | #cc99ff | RGB(204, 153, 255) | |
40 | #ffcc99 | RGB(255, 204, 153) | |
41 | #3366ff | RGB(51, 102, 255) | |
42 | #33cccc | RGB(51, 204, 204) | |
43 | #99cc00 | RGB(153, 204, 0) | |
44 | #ffcc00 | RGB(255, 204, 0) | |
45 | #ff9900 | RGB(255, 153, 0) | |
46 | #ff6600 | RGB(255, 102, 0) | |
47 | #666699 | RGB(102, 102, 153) | |
48 | #969696 | RGB(150, 150, 150) | |
49 | #003366 | RGB(0, 51, 102) | |
50 | #339966 | RGB(51, 153, 102) | |
51 | #003300 | RGB(0, 51, 0) | |
52 | #333300 | RGB(51, 51, 0) | |
53 | #993300 | RGB(153, 51, 0) | |
54 | #993366 | RGB(153, 51, 102) | |
55 | #333399 | RGB(51, 51, 153) | |
56 | #333333 | RGB(51, 51, 51) |
RGB-коды можно использовать в VBA Excel наряду с индексами цветов стандартной палитры, а HEX-коды — в HTML.
Процент черного в оттенках серого
Процентное соотношение черного цвета в оттенках серого:
Индекс | Наименование | Процент черного | Оттенок |
---|---|---|---|
1 | Черный | 100% | |
2 | Белый | 0% | |
15 | Серебряный | 25% | |
16 | Серый | 50% | |
48 | Светло-серый | 40% | |
56 | Темный уголь | 80% |
Вывод 56-цветной палитры на лист
Вывод на рабочий лист Excel 56-цветной палитры с помощью кода VBA:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
‘Функция, преобразующая десятичный код цвета в аргументы RGB Function DecToRGB(ByVal ColDec As Long) As String Dim r As Long Dim g As Long Dim b As Long r = (ColDec And &HFF&) 256 ^ 0 g = (ColDec And &HFF00&) 256 ^ 1 b = ColDec 256 ^ 2 DecToRGB = CStr(r) & «, « & CStr(g) & «, « & CStr(b) End Function ‘Вывод 56-цветной палитры на активный лист Sub Color56() Dim i As Integer For i = 1 To 56 Cells(i, 1) = i Cells(i, 2).Interior.ColorIndex = i Cells(i, 3).Value = «RGB(« & DecToRGB(Cells(i, 2).Interior.Color) & «)» Next End Sub |
Процедура Color56
записывает в ячейку первого столбца индекс цвета, ячейке второго столбца присваивается соответствующая индексу заливка, в ячейку третьего столбца записывается RGB-код соответствующего индексу цвета.
This Excel tutorial collects the VBA color code list for reference purpose, which includes ColorIndex, RGB color, VB color.
VBA Excel ColorIndex Property is to set color or get color for Objects like Cell color and Shape color. ColorIndex offers 56 basic colors plus the following special numbers.
ColorIndex Value | Explanation |
-4142 / xlColorIndexNone / xlNone | No fill |
-4105 / xlColorIndexAutomatic / xlAutomatic | Default color (white) |
Example 1: Set Cell A1 font color to red
Range("A1").Font.ColorIndex = 3
Example 2: Set Cell A1 back color to red
Range("A1").Interior.ColorIndex = 3
Example 3: Set Cell A1 border color to red
Range("A1").Borders.ColorIndex=3
Example 4: Get Cell A1 ColorIndex
col = Range("A1").Interior.ColorIndex
Excel RGB color
VBA Excel RGB Property is a color Property of Objects, commonly used for Cell color or Shape color.
“RGB” stands for Red Green Blue, which are known as three primary colors, which can be combined to produce other colors. For example, purple is a mix of blue and red. In RGB Property, you need to give a value (from 0 to 255) for each color in order to mix a new color. For example, Red = 255 means the brightness of color red is 100%.
If you try to change a color but you don’t know the color code, you can visit the websites below
http://www.ifreesite.com/color/
http://big-coronasdklua.blogspot.hk/2011/04/rgb.html
Example 1: Set Cell A1 font color to red
Range("A1").Font.Color = RGB(255, 0, 0)
Example 2: Set Cell A1 back color to red
Range("A1").Interior.Color = RGB(255, 0, 0)
Example 3: Set Cell A1 border color to red
Range("A1").Borders.Color = RGB(255, 0, 0)
Excel VB color
The simplest way to apply color is using the VB color name, you don’t have to remember which number represents which color, the the color for your choice is very limited.
Example: Set Cell A1 font color to red
Range("A1").Font.Color = vbRed
Colors
Colors are specified by selection or by using Hexadecimal and RGB codes.
Theme and Standard Colors
Excel has a set of themes and standard colors available for use. You select a color by clicking it:
The «More Colors» option allows you to select custom colors by entering a RGB or HEX code.
Hexadecimal Colors
Excel supports Hexadecimal color values
A hexadecimal color is specified with: #RRGGBB
.
RR (red), GG (green) and BB (blue) are hexadecimal integers between 00 and FF specifying the intensity of the color.
For example, #0000FF is displayed as blue, because the blue component is set to its highest value (FF) and the others are set to 00.
RGB Colors
Excel supports RGB color values.
An RGB color value is specified with: rgb( RED , GREEN , BLUE )
.
Each parameter defines the intensity of the color as an integer between 0 and 255.
For example, rgb(0,0,255) is rendered as blue, because the blue parameter is set to its highest value (255) and the others are set to 0.
Applying colors
Colors can be applied to cells, text and borders.
Colors are applied to cells by using the «Fill color» function.
How to apply colors to cells:
- Select color
- Select range
- Click the Fill Color button
The «Fill color» button remembers the color you used the last time.
Color options are explored by clicking on the arrow-down icon (),
next to the Fill color command:
The option allows selection of theme colors, standard colors or use of HEX or RGB codes by clicking on the More Colors option:
Colors are made from red, green, blue and are universal. Entering a color in one way will give you the code in the other. For example if you enter a HEX code, it will give you the RGB code for the same color.
Let’s try some examples.
Starting with theme and standard colors:
Great!
Try to apply the following colors:
- Theme color blue (accent 5) to A3:F3.
- Theme color green (accent 6) and A5:F5.
Did you make it?
Let’s apply colors using HEX and RGB codes.
Apply HEX code #ffcb05 to A7:F7:
Step by step:
- Select
A7:F7
- Open color options
- Click More colors
- Insert
#ffcb05
in the HEX input field - Hit enter
Notice that applying the HEX code gives the RGB code for the same color, and shows where that color is positioned on the color map.
Apply RGB code 60
, 90
, 166
to A9:F9
Step by step:
- Select
A9:F9
- Open color options
- Click More colors
- Insert
60
,90
,166
in the RGB input field - Hit enter
Notice that entering the RGB code also gives the HEX code and shows you where the color is positioned on the color map.
Well done! You have successfully applied colors using the standard, theme, HEX and RGB codes.
Do you want to find all of the VBA Excel Color Codes? This brief tutorial explains the different color codes for VBA Excel and then the VBA color conversion function.
The VBA color index list
A sample workbook with the codes written down can be downloaded from this link: https://www.cjoint.com/c/DIqlTjoC61G
What is the VBA color conversion function?
This function allows you to convert VBA colour codes from 1 to 56 (Interior.ColorIndex) to their long value (Interior.Color):
Function CouversionCouleurLong(Couleur As Integer) As Long Dim CouleursLng() CouleursLng = Array(0, 16777215, 255, 65280, 16711680, 65535, 16711935, 16776960, 128, 32768, 8388608, 32896, 8388736, 8421376, _ 12632256, 8421504, 16751001, 6697881, 13434879, 16777164, 6684774, 8421631, 13395456, 16764108, 8388608, 16711935, 65535, _ 16776960, 8388736, 128, 8421376, 16711680, 16763904, 16777164, 13434828, 10092543, 16764057, 13408767, 16751052, 10079487, _ 16737843, 13421619, 52377, 52479, 39423, 26367, 10053222, 9868950, 6697728, 6723891, 13056, 13107, 13209, 6697881, 10040115, 3355443, 16777215) 'En cas d'erreur colorie en noir If Couleur < 0 Or Couleur > 56 Then CouversionCouleurLong = 0 CouversionCouleurLong = CouleursLng(Couleur) End Function