Excel VBA, Color Dialog xlDialogEditColor

The xlDialogEditColor is a dialog used to get a color code from the user. Color codes are used to specify colors in VBA for Excel. For more information about working with colors please see Excel VBA Colors.

Jump To:

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

 

The dialog has 2 tabs:

  • Standard
  • Custom

Depending on the input parameters used, one of the tabs will be displayed upon displaying the dialog:

Standard Tab:

Excel VBA, DIalogs, Color, Tab1

Custom Tab:

Excel VBA, DIalogs, Color, Tab2


Syntax:

intResult = Application.Dialogs(xlDialogEditColor).Show(intIndex, [intRed], [intGreen], [intBlue])

intResult: Zero if the user cancels the dialog and -1 if the user selects a color.

There are 2 methods for calling this dialog:

  1. The standard tab is initially displayed.
  2. The custom tab is initially displayed.

If the user inputs the RGB values method 2 will be used. If the user omits the RGB values method one will be used.

intIndex: If the last 3 parameters are omitted, this will be the color index initially selected in the standard tab. This is a number between zero and 56.

If the last 3 parameters are not omitted, then the second method for displaying the dialog will be used:

intRed: The amount of Red in the color.

intGreen: The amount of Green in the Color

IntBlue: The amount of Blue in the color.


Examples:

The following line of code creates a color dialog, with the following characteristics:

  • The standard tab is the initially displayed
  • The color with the index 10 is initially selected

Application.Dialogs(xlDialogEditColor).Show(10)

Result:

Excel VBA, DIalogs, Color Example

The code below creates a color dialog with the following characteristics:

  • The custom tab is the initially displayed
  • The initially selected color has the following RGB parameters:
    • Red = 100
    • Green = 100
    • Blue = 200

Application.Dialogs(xlDialogEditColor).Show(10, 100, 100, 200)

Result:

Excel VBA, DIalogs, Color Example, 2
Note: In the second example the first parameter (10) has no effect.


Example 2, Set Fill Color:

In this example when the user presses the run button a color dialog will appear asking the user to select a color. The color chosen will be used as the fill color for cell A1:

Before:

Excel VBA, Dialogs, Color, Before

After pressing the Run button:

Excel VBA, Dialogs, Color, After Pressing the Run Button

After selecting a color:

Excel VBA, Dialogs, Color, After

The code used can be seen below:

Private Sub btnRun_Click()
Dim intResult As Long
'displays the color dialog
Application.Dialogs(xlDialogEditColor).Show(40, 100, 100, 200)
'gets the color selected by the user
intResult = ThisWorkbook.Colors(40)
'changes the fill color of cell A1
Range("A1").Interior.Color = intResult
End Sub

Note: The highlighted numbers must match.

The line below displays the color dialog:

Application.Dialogs(xlDialogEditColor).Show(40, 100, 100, 200)

The line below gets the color code of the color selected by the user:

intResult = ThisWorkbook.Colors(40)

The code below changes the fill color of the cell A1 to the color selected by the user:

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

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

See also:

 

If you need assistance with your code, or you are looking for a VBA programmer to hire feel free to contact me. Also please visit my website www.software-solutions-online.com

4 thoughts on “Excel VBA, Color Dialog xlDialogEditColor”

  1. Struggling to Excel says:

    This is Great. Thanks for sharing.

    1. pedrumj says:

      No problem, glad you found it helpful 🙂

  2. Kadr Leyn says:

    very thanks for sharing.
    I made template of changing background color with slider too .
    Here

  3. Aaron says:

    Having an issue with the code. Clicking Cancel in the color picker dialog still accepts whichever color is selected. I tried:
    If intResult = 0 Then
    Else
    intResult = ThisWorkbook.Colors(1)
    End If
    But now it registers both OK and Cancel as Cancel.

Leave a Reply

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