Excel VBA, ColorConstants - VBA and VB.Net Tutorials, Education and Programming Services

Excel VBA, ColorConstants

The class ColorConstants contains a list of color codes. Although this list is very limited but often it is enough to get the job done. If you require more specific colors please see Excel VBA, Color Code. In the example below the color code associated with each member of the class ColorConstants and its associated color is printed in column B and C:

Sub main()
Dim i As Integer
'black
i = 2
Cells(i, 1) = "vbBlack"
Cells(i, 2) = ColorConstants.vbBlack
Range(Cells(i, 3), Cells(i, 3)).Interior.Color _
= ColorConstants.vbBlack
'blue
i = 3
Cells(i, 1) = "vbBlue"
Cells(i, 2) = ColorConstants.vbBlue
Range(Cells(i, 3), Cells(i, 3)).Interior.Color _
= ColorConstants.vbBlue
'Cyan
i = 4
Cells(i, 1) = "vbCyan"
Cells(i, 2) = ColorConstants.vbCyan
Range(Cells(i, 3), Cells(i, 3)).Interior.Color _
= ColorConstants.vbCyan
'Green
i = 5
Cells(i, 1) = "vbGreen"
Cells(i, 2) = ColorConstants.vbGreen
Range(Cells(i, 3), Cells(i, 3)).Interior.Color _
= ColorConstants.vbGreen
'Magenta
i = 6
Cells(i, 1) = "vbMagenta"
Cells(i, 2) = ColorConstants.vbMagenta
Range(Cells(i, 3), Cells(i, 3)).Interior.Color _
= ColorConstants.vbMagenta
'Red
i = 7
Cells(i, 1) = "vbRed"
Cells(i, 2) = ColorConstants.vbRed
Range(Cells(i, 3), Cells(i, 3)).Interior.Color _
= ColorConstants.vbRed
'White
i = 8
Cells(i, 1) = "vbWhite"
Cells(i, 2) = ColorConstants.vbWhite
Range(Cells(i, 3), Cells(i, 3)).Interior.Color _
= ColorConstants.vbWhite
'Yellow
i = 9
Cells(i, 1) = "vbYellow"
Cells(i, 2) = ColorConstants.vbYellow
Range(Cells(i, 3), Cells(i, 3)).Interior.Color _
= ColorConstants.vbYellow
End Sub

Result:

Excel VBA, ColorConstants
The line below prints the name of the color constant in column A:

Cells(i, 1) = "vbBlack"

The line below prints the color code associated with the color constant in column B:

Cells(i, 2) = ColorConstants.vbBlack

The line below changes the fill color of the cell in column C to the color associated with the color constant:

Range(Cells(i, 3), Cells(i, 3)).Interior.Color _
= ColorConstants.vbBlack

You can download the file and code related to this article here.

See also:

Leave a Reply

Your email address will not be published. Required fields are marked *

More To Explore