Please Note: This article is written for users of the following Microsoft Excel versions: 97, 2000, 2002, and 2003. If you are using a later version (Excel 2007 or later), this tip may not work for you. For a version of this tip written specifically for later versions of Excel, click here: Determining the RGB Value of a Color.

Determining the RGB Value of a Color

Written by Allen Wyatt (last updated July 9, 2020)
This tip applies to Excel 97, 2000, 2002, and 2003


2

Neil uses colors a lot in his worksheets. He knows that he can generate a color based upon a numeric RGB value (and as explained in other ExcelTips issues). Neil would like to do the opposite—determine an RGB value. He wonders if there is a way to return (via function or macro) the RGB value of the color used to fill a cell.

Excel doesn't include a function to do this, but you can create your own user-defined function. The function you use depends on what you want to actually have returned to your worksheet. For instance, if you want to have the traditional six-character hex code for RGB colors returned, you would use the following very simple macro:

Function getRGB1(rcell) As String
    Dim sColor As String

    sColor = Right("000000" & Hex(rcell.Interior.Color), 6)
    getRGB1 = Right(sColor, 2) & Mid(sColor, 3, 2) & Left(sColor, 2)
End Function

This macro looks at the interior color for any cell you reference, puts the hex values for the color in the right order, and returns the string to Excel. To use the function you simply invoke it, in your worksheet, with a cell referenced in this manner:

=getRGB1(B4)

You may not want the traditional hex codes for the RGB colors, however. If you want to get the decimal values for each of the colors, then the following macro returns that:

Function getRGB2(rcell) As String
    Dim C As Long
    Dim R As Long
    Dim G As Long
    Dim B As Long

    C = rcell.Interior.Color
    R = C Mod 256
    G = C \ 256 Mod 256
    B = C \ 65536 Mod 256
    getRGB2 = "R=" & R & ", G=" & G & ", B=" & B
End Function

Invoked the same way as the getRGB1 macro, this version returns a string such as "R=255, G=204, B=0". You can also modify the macro even further so that it returns a single value, based upon a parameter you set:

Function getRGB3(rcell As Range, Optional opt As Integer) As Long
    Dim C As Long
    Dim R As Long
    Dim G As Long
    Dim B As Long

    C = rcell.Interior.Color
    R = C Mod 256
    G = C \ 256 Mod 256
    B = C \ 65536 Mod 256

    If opt = 1 Then
        getRGB3 = R
    ElseIf opt = 2 Then
        getRGB3 = G
    ElseIf opt = 3 Then
        getRGB3 = B
    Else
        getRGB3 = C
    End If
End Function

To use the macro, simply add a second parameter to the function used in your worksheet, specifying what you want:

=getRGB3(B4,1)

If the second parameter is 1, then the function returns just the red value. If you specify a second parameter of 2, then the green value is returned, and 3 returns the blue value. Any other value for the second parameter (or if you omit it entirely) returns the full decimal value of the interior color.

If you don't want to go the route of creating a macro, or if you want to determine colors in more than just your Excel worksheet, you might consider a third-party utility. One that looks interesting is Instant Eyedropper, which is free. You can find more information about it here:

http://instant-eyedropper.com

Note:

If you would like to know how to use the macros described on this page (or on any other page on the ExcelTips sites), I've prepared a special page that includes helpful information. Click here to open that special page in a new browser tab.

ExcelTips is your source for cost-effective Microsoft Excel training. This tip (10179) applies to Microsoft Excel 97, 2000, 2002, and 2003. You can find a version of this tip for the ribbon interface of Excel (Excel 2007 and later) here: Determining the RGB Value of a Color.

Author Bio

Allen Wyatt

With more than 50 non-fiction books and numerous magazine articles to his credit, Allen Wyatt is an internationally recognized author. He is president of Sharon Parq Associates, a computer and publishing services company. ...

MORE FROM ALLEN

Changing Macro Cell References Based on Edits

Place a cell reference in a macro, modify the structure of your worksheet, and you may soon find that the cell reference ...

Discover More

Printing a Macro List

Need a list of all the macros you've created? Word doesn't provide a way to create such a list, but you can use the ...

Discover More

Squeezing Everything In

Do you have just a line or two of text that 'spills over' onto another printed page? Here are some ways you can compress ...

Discover More

Solve Real Business Problems Master business modeling and analysis techniques with Excel and transform data into bottom-line results. This hands-on, scenario-focused guide shows you how to use the latest Excel tools to integrate data from multiple tables. Check out Microsoft Excel 2013 Data Analysis and Business Modeling today!

More ExcelTips (menu)

Transferring Data between Worksheets Using a Macro

Macros can be used for all sorts of data processing needs. One need that is fairly common is the need to move data from ...

Discover More

Deleting a Macro

Don't need that old macro any more? Here's how to get rid of it so that it is no longer a part of your workbook.

Discover More

Running Macros on Hidden Worksheets

Excel allows you to hide worksheets so that they aren't visible to those using your workbook. Hiding worksheets has a ...

Discover More
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

View most recent newsletter.

Comments

If you would like to add an image to your comment (not an avatar, but an image to help in making the point of your comment), include the characters [{fig}] (all 7 characters, in the sequence shown) in your comment text. You’ll be prompted to upload your image when you submit the comment. Maximum image size is 6Mpixels. Images larger than 600px wide or 1000px tall will be reduced. Up to three images may be included in a comment. All images are subject to review. Commenting privileges may be curtailed if inappropriate images are posted.

What is 8 - 5?

2016-05-25 05:26:33

udaya

RGB2 is very usefull.thanks


2015-10-10 08:21:40

sheldon hopkins

If the question is "What is the RGB value for an existing cell the simplest method is:

Click on the cell
Right click the cell
In the pulldown list, click 'fill'
In the next pulldown, click 'more colors'
and finally click on 'standard'
Your RGB numbers are shown.


This Site

Got a version of Excel that uses the menu interface (Excel 97, Excel 2000, Excel 2002, or Excel 2003)? This site is for you! If you use a later version of Excel, visit our ExcelTips site focusing on the ribbon interface.

Newest Tips
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

(Your e-mail address is not shared with anyone, ever.)

View the most recent newsletter.