Calling and using the color picker with VBA

When building Excel-based tools and applications it is often useful for the user to select a color.  Whilst it is possible to build a custom UserForm, Excel’s native Color window is more than suitable for most circumstances.

Excel’s native color picker:

Color Picker Standard Color Picker Custom

To show the Color window, you might think we could use the Macro Recorder, but it doesn’t quite give us what we need.  The recorded code will show the colors selected, but not the action of displaying the Colors window.

Recorded macro code – color picker will not display:

 With Selection.Interior
     .Pattern = xlSolid
     .PatternColorIndex = xlAutomatic
     .Color = 3166746
     .TintAndShade = 0
     .PatternTintAndShade = 0
 End With

This post provides the VBA code required to call the Colors window and apply the user’s selection.

Using the Edit Color Dialog Box

The VBA code to call the Color window is straightforward.

Each window in Excel has it’s own Dialog setting which can be called.  Typing Application.Dialogs( into the Visual Basic Editor will display the list of available dialog boxes.

Color Picker - DialogBox Options

Any of these windows can be called.  The code below will open the Add-In Manager window.


To call the Color Window we would reference  xlDialogEditColor instead of xlDialogAddinManager.

The xlDialogEditColor option does require one argument.  There are 56 color positions available in Excel; when calling the Color window it is necessary to state the which color position to be edited.  The code below shows how to edit the twelfth color position.

Application.Dialogs(xlDialogEditColor).Show (12)

Using a position number higher than 56 will result in an error:

Color Picker - Color Position > 56

It is acceptable to provide the single color position argument, but it may be better to set the default color value when the window opens.

Application.Dialogs(xlDialogEditColor).Show 1, 26, 82, 48

The code above sets color position 1 to the color with RGB value of

  • Red: 26
  • Green: 82
  • Blue: 48

That color is a dark green (just in case you were wondering).

The Color window has two tabs, Standard and Custom (see the images at the top of this article).  If the RGB color code matches a color from the Standard tab the Standard tab will display, else the Custom tab will display.

Full VBA example

The code below, which should be used in a standard Module will:

  • Obtain the color code of a cell with the Named Range of RGBColor
  • Assign the color code to a variable called FullColorCode
  • Use formulas to split the full-color code into it’s Red, Green and Blue components
  • Display the Edit Color Dialog Box, setting the default to the Red, Green and Blue values above
  • On clicking OK, the selected color is assigned to the FullColorCode variable
  • The cell named RGBColor is updated with the new color code from the FullColorCode variable.
Sub ColorDialog()

'Create variables for the color codes
Dim FullColorCode As Long
Dim RGBRed As Integer
Dim RGBGreen As Integer
Dim RGBBlue As Integer

'Get the color code from the cell named "RGBColor"
FullColorCode = Range("RGBColor").Interior.Color

'Get the RGB value for each color (possible values 0 - 255)
RGBRed = FullColorCode Mod 256
RGBGreen = (FullColorCode \ 256) Mod 256
RGBBlue = FullColorCode \ 65536

'Open the ColorPicker dialog box, applying the RGB color as the default
If Application.Dialogs(xlDialogEditColor).Show _
    (1, RGBRed, RGBGreen, RGBBlue) = True Then

    'Set the variable RGBColorCode equal to the value
    'selected the DialogBox
    FullColorCode = ActiveWorkbook.Colors(1)
    'Set the color of the cell named "RGBColor"
    Range("RGBColor").Interior.Color = FullColorCode

    'Do nothing if the user selected cancel

End If

End Sub

Adapting the code

The example above uses a worksheet cell to hold the color.  It is possible to adapt this technique with any objects with a .Color property.  This is not just restricted to worksheets, but to many, many objects within the VBA object model.

5 thoughts on “Calling and using the color picker with VBA”

    • Just an idea…

      If you create your own color palette (Page Layout -> Themes -> Colors). You could then load the palette into each workbook using something like the following:

      ActiveWorkbook.Theme.ThemeColorScheme.Load ("C:\Program Files (x86)\Microsoft Office\Root\Document Themes 16\Theme Colors\Blue.xml")

Leave a Comment