Neil uses colors a lot in his worksheets. He knows that he can generate a color based upon a numeric RGB value (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. (Neil wants the actual color applied to the cell, not any "override" color, such as one imposed by a conditional format.)
Excel doesn't include a function to do this, but if you only need to check out the RGB values for a single cell, the easiest way is to follow these steps:
Figure 1. The Custom tab of the Colors dialog box.
If you have a need to get the values more often, then creating your own user-defined function is the way to go. 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:
ExcelTips is your source for cost-effective Microsoft Excel training. This tip (10180) applies to Microsoft Excel 2007, 2010, 2013, 2016, 2019, and Excel in Office 365. You can find a version of this tip for the older menu interface of Excel here: Determining the RGB Value of a Color.
Create Custom Apps with VBA! Discover how to extend the capabilities of Office 2013 (Word, Excel, PowerPoint, Outlook, and Access) with VBA programming, using it for writing macros, automating Office applications, and creating custom applications. Check out Mastering VBA for Office 2013 today!
You can use macros to make your common Excel tasks easier and faster. For instance, if you routinely need to create new ...
Discover MoreYou can easily add a button to your worksheet that will allow you to run various macros. This tip shows how easy it is.
Discover MoreMacros are very powerful, but you may not want them to always be available to a user. Here are some ways you can limit ...
Discover MoreFREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
2020-10-16 08:21:40
Roma
How to do the same for Excel online (for web)?
2020-07-13 03:39:00
Your old resurrections are always welcome, Rick
2020-07-12 04:46:37
Rick Rothstein
I know I am quite late to this thread, but I just had to post this. In the fourth message (dated 20140-11-12), Willy Vanhaelen posted a "much shorter" version of the getRGB3 function. For some reason, his backslashes did not print, so it is likely mine might not either (if it doesn't, it goes between the word "Color" and the 2). With that said, here is an even much shorter version... it is a one-liner!
Function getRGB3(rcell As Range, Optional opt As Integer = 1) As Long
getRGB3 = rcell.Interior.Color \ 2 ^ (8 * (opt - 1)) Mod 256
End Function
Depending on how you call this (and Willy's) function, it might be necessary to qualify the rcell and opt variables with the keyword ByVal.
2020-01-30 05:05:14
Damien DAMBRE
many thanks for this one. :) it's quite useful when you have tables where the status is defined by green-yellow-red.
2019-12-24 13:02:19
lew b
When I clicked on the link for eyedropper my malware program blocked it saying it contained Trojan
2018-05-12 09:42:13
Marsel
Hello guys .. I have a question : how to convert HSL values to RGB values in Excel using VBA?
if you have any idea or code please help me..
thanks for everything
2018-04-17 11:40:24
Peter Atherton
Ibnu
Add the first line above the 2nd
Application.Volatile
C = rcell.Interior.Color
2018-04-16 08:10:42
Ibnu
how to make that funcion auto run when the valeu cell change?
2017-06-17 11:35:13
James Duffy
First let me complement you for a very useful website.
Does this also work for the font color? If so, I am having difficulty applying these techniques to font colors. For example, the Custom Colors suggestion when applied to fonts does not produce any result for me in Excel 2016. Just 0s for the RGB values regardless of the color of the font. Modifying the macros to refer to Font in lieu of Interior behaves similarly. I want to develop some vba code that would place values in an adjacent cell that is triggered by the font color of the trigger cell. Specifically, for a list of stocks with colored fonts (vbBlue for Bull and vbRed for Bear), I want the trigger to be the Blue or Red font color. If it does apply, any suggestions as to how to make it work would be much appreciated. Thank you in advance for any help you can provide.
2017-05-03 17:59:11
PReinie
If you want to make one cell similar to another, format painter may help, unless you want only part of the format the same.
I do have conditional formatting applied to some of the cells (conditional formatting - duplicates).
How do I find those formatted cells? In other words, I want the background color of the cells so I can use Find on them.
Never mind - I found it (pun included). The Find command's "Format" has the option to pick format from cell. Good. Now I don't need the RGB!
2017-04-24 15:26:52
Kane
I converted the xls file to xlsm so I could use your first macro at the top of this page. Why is it that as soon as I create the macro, even though in Advanced Options I ensured the box alongside, "show formulas in cells instead of their calculated results" is unclicked - all formulas are now shown as formulas, not the calculated results? This is true even after I delete the macro. The only way to undo it is by using a previous version of the file.
2017-01-28 04:59:21
Rob
Thanks - really useful to me.
2016-11-09 18:36:41
Gusti NS
Thanks so much, getRGB1 was very very helpful.
Gusti
2016-10-14 07:01:12
Ibukunoluwa Aina
Thanks so much for the tip. It was very helpful.
2016-04-26 08:10:46
J
Thank you, getRGB2 has been really useful to me.
Thanks a lot.
J
2015-02-03 02:21:10
Frederick
Hi, thank you for the getRGB1 function. It's exactly what I was looking for. First I a similar function on another site but it did not return the correct value. Your function, however, works perfectly :-)
Thank again. Frederick
2014-11-13 10:42:55
@ Willy
You are absolutely right, but hard-coding each assignment, cell-by-cell, would be more code and more maintenance over the development and operational life of the project, right?
I use a lot of what I call "formatting by exemplar" (using the font/colors/borders etc from one cell and applying it to others) so I converted my standard Sub by making the parameters into variables, adding the Set assignments, and removing all of the other formatting that can be passed from one cell to another.
By using the exemplar formatting approach I can have users, using Excel, show me exactly what they want to see as far as format is concerned. I can give them an first cut layout. from that point many of them can work through the process. With others, where I have to drive the mouse, it eliminates a lot of note-taking in meetings as well as "Could you make it light/darker greener/bluer then get back with me?" discussions.
On the back end, give those cells meaningful Names, e.g. on one project I have "fmtReleaseBanner" and "fmtNotesHeader" then I can pass the named range and the target range to one subroutine that performs the same action, whether it is in the NewMonth, UpdateForMeeting, or PublishResults main Sub.
Just my opinion!
2014-11-12 10:45:33
Willy Vanhaelen
Here is a much shorter version of the getRGB3 function in this tip:
Function getRGB3(rcell As Range, Optional opt As Integer) As Long
Dim C As Long
C = rcell.Interior.Color
Select Case opt
Case 1: getRGB3 = C Mod 256
Case 2: getRGB3 = C 256 Mod 256
Case 3: getRGB3 = C 65536 Mod 256
Case Else: getRGB3 = C
End Select
End Function
2014-11-12 10:44:16
Willy Vanhaelen
@ Bigger Don
If you put the macro in the sheet's code page you can reduce it to one line:
Sub copycolor()
Range("A3").Interior.Color = Range("D3").Interior.Color
End Sub
2014-11-11 12:47:50
@ James
Why worry about the Hex value unless you want to manipulate it before assigning it to another cell? You can simply assign one cell's Interior.Color directly to another.
Sub copycolor()
Dim rng1 As Range
Dim rng2 As Range
Set rng1 = Worksheets("Sheet1").Range("D3")
Set rng2 = Worksheets("Sheet1").Range("A3")
rng2.Interior.Color = rng1.Interior.Color
End Sub
2014-11-10 22:01:29
James Wilson
I don't want to get the RGB, I want to set the RGB. Read the sColor hex from a cell and set that as the background colour of the cell "Asking".
Cheers Jim
Got a version of Excel that uses the ribbon interface (Excel 2007 or later)? This site is for you! If you use an earlier version of Excel, visit our ExcelTips site focusing on the menu interface.
FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
Copyright © 2021 Sharon Parq Associates, Inc.
Comments