Convert A Cell's Interior Color Code to RGB Format
When you issue the following command from the Immediate Windows of the VBE (Visual Basic Environment), you receive a long integer which defines the color of the cell:
? ActiveCell.Interior.Color
At times it is useful to convert that long integer into the RGB format so that, for example, you can assign a cell's interior color using a statement such as ActiveCell.Interior.Color = RGB(238,44,189)
The following code illustrates the method to convert from the long integer to RGB:
Program Code
Option Explicit Public Sub ConvertColorCodeToRGB() ' ****************************************************************** ' Select the Cell From Which You Want to Know the RGB Color Values ' Then Run This Macro To Display the RGB Values ' ' The Code in this macro could be condensed but was written in ' "full form" to allow easy viewing of all processes. ' ****************************************************************** ' ****************************************************************** ' Note: In Excel 2003, you can only set a limited number of colors ' using ActiveCell.Interior.Color = RGB(Red, Green, Blue) ' where Red, Green and Blue each range from 0 to 255 decimal. ' This limitation is due to the limits of pallet colors. ' ' In Excel 2007 and above, you can set any combination of ' Red, Blue and Green colors (Over 16 million colors) ' ****************************************************************** Dim lngColorIntegerCode As Long Dim strHexCode As String Dim strFullHexCode As String Dim intFillCharacters As Integer Dim intRed As Integer Dim intGreen As Integer Dim intBlue As Integer Dim intLengthOfHex As Integer ' ****************************************************************** ' Get The Internal Excel Color Code Of The Cell ' ****************************************************************** lngColorIntegerCode = ActiveCell.Interior.Color ' ****************************************************************** ' Convert That Code To Hex ' ****************************************************************** strHexCode = Hex(lngColorIntegerCode) ' ****************************************************************** ' Determine How Many Fill Zeroes Are Required For 6 Digit Hex Code ' ****************************************************************** intFillCharacters = 6 - Len(strHexCode) ' ****************************************************************** ' Create a full six digit Hex color code ' Blue Occupies positions 1-2 ' Green Occupies positions 3-4 ' Red Occupies positions 5-6 ' ****************************************************************** strFullHexCode = String(intFillCharacters, "0") & strHexCode ' ****************************************************************** ' Extract Each Color Code ' ****************************************************************** intRed = CInt("&H" & Right(strFullHexCode, 2)) intGreen = CInt("&H" & Mid(strFullHexCode, 3, 2)) intBlue = CInt("&H" & Left(strFullHexCode, 2)) ' ****************************************************************** ' Display Colors ' ****************************************************************** MsgBox ("Red = " & intRed & vbCrLf & _ "Green = " & intGreen & vbCrLf & _ "Blue = " & intBlue) End Sub