0% found this document useful (0 votes)
111 views2 pages

Colours in Vba

Uploaded by

Dario Montoya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
111 views2 pages

Colours in Vba

Uploaded by

Dario Montoya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Chapter xx - Colours in VBA

COLOURS IN VBA

Excel Colours

Excel VBA has a surprisingly large number of ways to change the colours of various objects. This
chapter briefly summarises the main techniques using the fill colour of a cell as an example.

The Two Colour Properties of a Range

There are two properties that you can change to affect the fill colour of a range object, as shown in
the diagram below:

You can change the fill colour of a range


using either the Color or the ColorIndex
property.

The table below summarises the differences between the two properties:

Property Description Range


Color The modern way to set colours using the full range of RGB colours. The RGB 0 to 16777215
value is consistent across workbooks and Office applications.
ColorIndex A legacy feature of Excel allowing you to choose from the limited set of colours 1 to 56
defined in the workbook.

Wise Owl’s Hint


To clear the fill colour from a range of cells you can use the following code:

ActiveCell.Interior.ColorIndex = xlColorIndexNone

The ColorIndex Colours

You can view a workbook’s ColorIndex colours by choosing File | Options from the Excel ribbon.

On the Save page of the


dialog box click this button
to see the workbook’s
colours.
You can change any of the default
colours by selecting it in the grid, clicking
this button and then choosing an
alternative colour.
Each of the colours in the grid
corresponds to one of the 56 numbers
of the ColorIndex property, although
sadly not in a predictable order!

© Copyright 2021 Page 1


Chapter xx - Colours in VBA

Colour Numbers and Names

The Color property has 16,777,216 different values! Fortunately, you don’t need to remember
which number represents which colour as there are several sets of named colours in Excel VBA.

VBA Colour Constants

VBA has eight generic colour constants which you can use in any Office application.

You can choose from the list


of ColorConstants or just
refer to the constant
directly.
The only value missing from
the list in the image is
vbYellow.

Excel’s RGB Constants

Excel has its own specific list of named colours, all of which begin with the letters rgb.

There are 134 different colours in this list, many


with a wonderfully flowery name. This owl’s
favourites are rgbPapayaWhip and
rgbLavenderBlush.

The RGB Function

The RGB function allows you to calculate the number of any colour by mixing different amounts of
red, green and blue. This is a common technique used by companies to define brand colours.

Each of the three parameters can be a whole number


between 0 and 255. This gives 256^3 combinations which
just so happens to equal 16,777,216 i.e. the range of
values you can use for the Color property!

You may be familiar with the RGB system from dialog


boxes like this one. You can find the amount of red, green
and blue used to make any colour and then replicate
these numbers in your code to obtain the same colour.

© Copyright 2021 Page 2

You might also like