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:
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.
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.
Using a position number higher than 56 will result in an error:
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 Else '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.
Get our FREE VBA eBook of the 30 most useful Excel VBA macros.
Automate Excel so that you can save time and stop doing the jobs a trained monkey could do.
If you’ve found this post useful, or if you have a better approach, then please leave a comment below.
Do you need help adapting this to your needs?
I’m guessing the examples in this post didn’t exactly meet your situation. We all use Excel differently, so it’s impossible to write a post that will meet everybody’s needs. By taking the time to understand the techniques and principles in this post (and elsewhere on this site) you should be able to adapt it to your needs.
But, if you’re still struggling you should:
- Read other blogs, or watch YouTube videos on the same topic. You will benefit much more by discovering your own solutions.
- Ask the ‘Excel Ninja’ in your office. It’s amazing what things other people know.
- Ask a question in a forum like Mr Excel, or the Microsoft Answers Community. Remember, the people on these forums are generally giving their time for free. So take care to craft your question, make sure it’s clear and concise. List all the things you’ve tried, and provide screenshots, code segments and example workbooks.
- Use Excel Rescue, who are my consultancy partner. They help by providing solutions to smaller Excel problems.
Don’t go yet, there is plenty more to learn on Excel Off The Grid. Check out the latest posts: