As previously mentioned in the article Excel VBA, Color Code when working with colors in Excel you must know their colorcode. Though sometimes you may want set the color by using the RGBcolor model. This can be achieved using the RGB() function. For more information about the RGBmodel please see RGB Color Model.
The RGB() function receives 3 integers as input. The amount of Red, Green and Blue in the color. The values are between 0 and 255. The RGB() function returns the colorcode associated with the input parameters.
ColorCode = RGB(Red, Green, Blue)
Red: Integer determining the amount of Red in the color (0~255)
Green: Integer determining the amount of Green in the color (0~255)
Blue: Integer determining the amount of Blue in the color(0~255)
ColorCode: A Long value determining the colorcode assiociated with the input parameters.
In this example the user inputs the amount of Red, Blue and Green in cell A2, B2 and C2. The fill color of cell D2 is updates with the colorcode returned by the RGB() function. Below you can see some examples:
Red = 0, Green = 0, Blue = 0:
Red = 255, Green = 0, Blue = 0:
Red = 255, Green = 255, Blue = 0:
Red = 255, Green = 115, Blue = 115:
The program uses a Worksheet_Change event handler. The event handler executes whenever the user inputs a new value:
'executes whenever the user inputs new values in the sheet Private Sub Worksheet_Change(ByVal target As Range) 'changes the color of cell D2 to the color determined 'by the red, green and blue parameters Range("D2").Interior.Color = RGB(Cells(2, 1), _ Cells(2, 2), Cells(2, 3)) End Sub
You can download the file and code related to this article here.