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:
Custom Tab:
Contents
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:
- The standard tab is initially displayed.
- 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:
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:
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:
After pressing the Run button:
After selecting a color:
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”