VBA Color Constants

Simplify using basic colours

In Excel colours are colors. If you want to used basic colours you can use the built-in VBA color constants. This simplifies the code and makes it more descriptive.

To demonstrate the basic color constants I have placed them in cells on the spreadsheet – see image below.

I created some code to go through and apply the color constants. The code is shown below.

Sub Colours()
 
Dim c, vbColour
 
For Each c In Selection
 
    Select Case c
        Case "vbBlack"
            vbColour = vbBlack
        Case "vbBlue"
            vbColour = vbBlue
        Case "vbCyan"
            vbColour = vbCyan
        Case "vbGreen"
            vbColour = vbGreen
        Case "vbMagenta"
            vbColour = vbMagenta
        Case "vbRed"
            vbColour = vbRed
        Case "vbWhite"
            vbColour = vbWhite
        Case "vbYellow"
            vbColour = vbYellow
    End Select
 
    c.Offset(0, 1).Interior.Color = vbColour
 
Next c
 
End Sub

The vbColour variable captures the color constant. That colour is applied to the cell on the right of selection using the Offset statement.

Normally VBA refers to colours by a unique number. Using the VBA color constant is more descriptive. Unfortunately, these are the only colours available as color constants.

You can see the result of running the macro in the image below.

To apply a specific colour you can use something like the code below.

Range("A1").Interior.Color = vbRed

 

Please note: I reserve the right to delete comments that are offensive or off-topic.

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.