Please Note: This article is written for users of the following Microsoft Excel versions: 2007 and 2010. If you are using an earlier version (Excel 2003 or earlier), this tip may not work for you. For a version of this tip written specifically for earlier versions of Excel, click here: Colors in an IF Function.

Colors in an IF Function

Written by Allen Wyatt (last updated May 25, 2023)
This tip applies to Excel 2007 and 2010


43

Steve would like to create an IF statement (using the worksheet function) based on the color of a cell. For example, if A1 has a green fill, he wants to return the word "go", if it has a red fill, he wants to return the word "stop", and if it is any other color return the word "neither". Steve prefers to not use a macro to do this.

Unfortunately, there is no way to acceptably accomplish this task without using macros, in one form or another. The closest non-macro solution is to create a name that determines colors, in this manner:

  1. Select cell A1.
  2. Click Insert | Name | Define. Excel displays the Define Name dialog box.
  3. Use a name such as "mycolor" (without the quote marks).
  4. In the Refers To box, enter the following, as a single line:
  5.      =IF(GET.CELL(38,Sheet1!A1)=10,"GO",IF(GET.CELL(38,Sheet1!A1)
         =3,"Stop","Neither"))
    
  6. Click OK.

With this name defined, you can, in any cell, enter the following:

=mycolor

The result is that you will see text based upon the color of the cell in which you place this formula. The drawback to this approach, of course, is that it doesn't allow you to reference cells other than the one in which the formula is placed.

The solution, then, is to use a user-defined function, which is (by definition) a macro. The macro can check the color with which a cell is filled and then return a value. For instance, the following example returns one of the three words, based on the color in a target cell:

Function CheckColor1(range)
    If range.Interior.Color = RGB(256, 0, 0) Then
        CheckColor1 = "Stop"
    ElseIf range.Interior.Color = RGB(0, 256, 0) Then
        CheckColor1 = "Go"
    Else
        CheckColor1 = "Neither"
    End If
End Function

This macro evaluates the RGB values of the colors in a cell, and returns a string based on those values. You could use the function in a cell in this manner:

=CheckColor1(B5)

If you prefer to check index colors instead of RGB colors, then the following variation will work:

Function CheckColor2(range)
    If range.Interior.ColorIndex = 3 Then
        CheckColor2 = "Stop"
    ElseIf range.Interior.ColorIndex = 4 Then
        CheckColor2 = "Go"
    Else
        CheckColor2 = "Neither"
    End If
End Function

Whether you are using the RGB approach or the color index approach, you'll want to check to make sure that the values used in the macros reflect the actual values used for the colors in the cells you are testing. In other words, Excel allows you to use different shades of green and red, so you'll want to make sure that the RGB values and color index values used in the macros match those used by the color shades in your cells.

One way you can do this is to use a very simple macro that does nothing but return a color index value:

Function GetFillColor(Rng As Range) As Long
    GetFillColor = Rng.Interior.ColorIndex
End Function

Now, in your worksheet, you can use the following:

=GetFillColor(B5)

The result is the color index value of cell B5 is displayed. Assuming that cell B5 is formatted using one of the colors you expect (red or green), you can plug the index value back into the earlier macros to get the desired results. You could simply skip that step, however, and rely on the value returned by GetFillColor to put together an IF formula, in this manner:

=IF(GetFillColor(B5)=4,"Go", IF(GetFillColor(B5)=3,"Stop", "Neither"))

You'll want to keep in mind that these functions (whether you look at the RGB color values or the color index values) examine the explicit formatting of a cell. They don't take into account any implicit formatting, such as that applied through conditional formatting.

For some other good ideas, formulas, and functions on working with colors, refer to this page at Chip Pearson's website:

http://www.cpearson.com/excel/colors.aspx

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 (10780) applies to Microsoft Excel 2007 and 2010. You can find a version of this tip for the older menu interface of Excel here: Colors in an IF Function.

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

Comments in Text Boxes

If you use text boxes in your documents, you may sometime want to place a comment in the text box, the same as you can do ...

Discover More

Protecting Conditional Formatting

When you apply conditional formatting to the cells in your worksheet, those rules can seem a bit fragile at times. For ...

Discover More

Rounding Time

Need to round the time in a cell to a certain value? There are a couple of ways you can do this with a formula.

Discover More

Excel Smarts for Beginners! Featuring the friendly and trusted For Dummies style, this popular guide shows beginners how to get up and running with Excel while also helping more experienced users get comfortable with the newest features. Check out Excel 2013 For Dummies today!

More ExcelTips (ribbon)

Exact Matches with DSUM

The DSUM function is very handy when you need to calculate a sum based on data that matches criteria you specify. If you ...

Discover More

Making VLOOKUP Trigger a Macro

VLOOKUP is an oft-used worksheet function to lookup values in a data table. If the function cannot return a value, it ...

Discover More

Using the FORECAST Function

Excel provides a handy worksheet function that allows you to forecast values based upon a set of known values. This ...

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 four minus 0?

2022-03-05 11:37:23

J. Woolley

@Eileen Endres
Did you read the Tip?


2022-03-04 09:49:40

Eileen Endres

If I wanted to make a macro to recognize the greatest, most recent column with a green fill color in a cell, and report it back in a certain column/cell, how would I go about such?
It would need to iterate through a whole data set (multiple rows/columns)

(see Figure 1 below)

Is there anything I can reference for such?

Regards,

Figure 1. 


2021-02-27 07:48:55

Rox Den

Function CheckColor1(range)
If range.Font.Color = RGB(255, 0, 0) Then
CheckColor1 = "No Req $"
ElseIf range.Font.Color = RGB(84, 130, 53) Then
CheckColor1 = "Tx $ Sent"
ElseIf range.Font.Color = RGB(112, 48, 160) Then
CheckColor1 = "New Req $"
Else
CheckColor1 = "TBC"
End If
End Function

or

How to fill a cell with text using if else based on cell color fill.

To elaborate on that,

If I have 2 columns, A and B

In column A, there are random digits for every row, but that's not what matters. What matters is the cell color fill of that cell.

So if A1 is color yellow, insert "sample1" in B1,
else, insert "sample2" in B1

I would appreciate any help on how I can achieve this.


2020-12-05 10:25:49

kashmira

I use conditional formatting to color the cells. And I want return value in adjacent cell. If above dies not work for implicit formatting, is there a way around to get solution for this. ?

Thanks
Kashmira


2020-12-03 20:16:43

Christian Quintero

Thanks a lot for this explanaition. It worked like a charm (even having the cells highligted using conditional formatting), but first you have to transform the "implicit" highligted format given by the conditional formatting to a explicit format, otherwise the formula won't recognized the color. You can do this by following the steps mentioned in this other post:

https://excelribbon.tips.net/T013721_Converting_Conditional_Formatting_to_Regular_Formatting.html

PD. When you save the file like a .html, you will lose ALL FORMAT, so keep that in mind so you don't do more damage.


2020-11-27 04:23:08

Satinder Randhawa

Hi Allen,

I had a file with various coloured boxes containing numbers in for example each row will have 5 grey cells 2 green 3 red and im looking for a formula to bring me back the value in the last grey box per row? I hope this makes sense if not I can show you my spreadsheet?

Thanks,
Sat


2020-09-12 08:43:37

Peter Atherton

Aldrin C.
Presumably, the cell colour is based on the Random numbers. If that is the case use a lookup formula in column B to a table with Colour as the lookup value and the value in the table's second column.


2020-09-11 06:11:13

Aldrin C.

Hi I am really a beginner in Excel, and there's a lot of steps I feel are skipped in the tutorial. I assume this is for advanced users. Anyways, I'd hope if anyone can teach me this:

How to fill a cell with text using if else based on cell color fill.

To elaborate on that,

If I have 2 columns, A and B

In column A, there are random digits for every row, but that's not what matters. What matters is the cell color fill of that cell.

So if A1 is color yellow, insert "sample1" in B1,
else, insert "sample2" in B1

I would appreciate any help on how I can achieve this.


2020-06-30 14:21:26

Chuck T

Thanks for the tips on the getfillcolor() function. I note that it doesn't work if the cell color is based on a conditional formatting rule. Is there a function that will return the cell color that is the result of a conditional formatting rule? Thanks!


2020-05-14 03:21:31

Stef

For anyone still looking for an answer to this, the link below works a treat! Took me 2 minutes and did EXACTLY what I needed in allowing me to sum cells based on their colour.

https://www.exceltrick.com/how_to/sum-cells-based-on-background-color/?


2020-02-11 17:42:05

Ruthie A. Ward

Hi Terry. I can't quite picture what you're trying to do. Would you be willing to send a file to me? Maybe I can help.


2020-02-07 18:37:11

Terry

I have a linear spreadsheet (bar graph) that has different colors depicting different functions during the maintenance timeline. I am trying to have the colors = a number for resource forecasting on a separate line below the graph that will show a daily total. any ideas would be appreciated.


2020-02-04 05:17:02

YossiD

Rami, did you find a way to get the result to update dynamically?


2019-07-17 08:28:44

Richard Sylvain Joseph

I modified it without trying the original one. The modified version worked like a breeze. Thanks a lot.
================================================
Function CheckColor1(range)
If range.Font.Color = RGB(255, 0, 0) Then
CheckColor1 = "No Req $"
ElseIf range.Font.Color = RGB(84, 130, 53) Then
CheckColor1 = "Tx $ Sent"
ElseIf range.Font.Color = RGB(112, 48, 160) Then
CheckColor1 = "New Req $"
Else
CheckColor1 = "TBC"
End If
End Function
===============================================

Once more thanks a bunch.

Richard.


2019-06-21 09:07:20

rami khater

Dear Allen

thank you so much for the color tip, it worked and i was able to get the color of the cell, however it dosen't dynamically change if i change the cell color, if there anyting i can do to make dynamic?

thank you so much

Rami


2018-05-31 08:39:32

Ruthie A. Ward

Hi Barrett. I just found this post and was intrigued with your question. I can think of 2 ways to accomplish this. First you can split the result so that the first zero appears in a different column and color all that column's values red. Second you can use a macro to convert the formula results to a value and then use a loop to modify the first character's color in each cell. Perhaps the newest version of Excel has a better option, but I only have 2010.


2018-05-02 04:04:55

Mostafa`

Thank you so much. this really helped.


2018-03-01 18:37:01

Larry Spiegel

Hi Allen:

I am trying to do the following to make a simple configurator:
I populated Column A with the character "N". Column B contains upper level assembly numbers. If a cell in A is changed to Y, the corresponding part numbers from various cells in Columns C though F, say, have their fill color changed to yellow using Conditional Formatting.
What I would like to do then is use LOOKUP, MATCH or whatever to extract the text (part numbers) from those cells that are yellow and export them to another Column, Worksheet, or text file, etc.
Can you help with that?


2017-10-19 19:51:56

Darrin

I am trying to streamline how I track overtime a work. Currently I go in the monthly excel calendar and highlight the cell with a specific color indicating that they worked a days OT or Mids OT. Then type the name of that person in an spreadsheet to post on the employee information board I created further down in the workbook. Is there a way when I type the name in on the sign up sheet it would automatically highlight the date cell in a specific color to indicate on our calendar that they worked a days or a mids OT?

Thank you. Way out of my area of knowledge


2017-08-23 18:02:04

Lynndoom

Thank you for the explination. I am not certian how to apply it however. I need to perform a simple task.
I need to add the dollar amounts filled yellow in the same collumn.


2017-06-19 14:56:32

Barrett

=COUNTIF(R:R,"?????")&("/")&COUNTIFS(R:R,"?????",I:I,"x")&("/")&COUNTIFS(R:R,"?????",J:J,"x")&("/")&COUNTIFS(R:R,"?????",L:L,"x")&("/")&COUNTIFS(R:R,"?????",P:P,"Insurance")&("/")&COUNTIFS(R:R,"?????",P:P,"International")&("/")&COUNTIFS(R:R,"?????",P:P,"Both")&("/")&COUNTIFS(R:R,"?????",Q:Q,"yes")

I am using this formula. My result is 0/0/0/0/0/0/0/0

What i would like is for the 1st 0 to be is red and the rest in black is that possible.


2017-01-19 06:20:42

David Houston

Hi,

I've used the following VBA that you highlighted in Excel 201):

Function GetFillColor(Rng As range) As Long
GetFillColor = Rng.Interior.ColorIndex
End Function

When I manually update a cell the VBA picks up the colour code. It does not work however where the cell is conditionally formatted i.e. returning a duplicate value in a column colours the cell red. This VBA still treats the cell as blank in fill and returns the number '-4142'.

Any idea how to update this?


2016-11-23 13:50:51

Jeff

I am looking for a function that will compare 2 columns of numbers and give me a total count (4 sets of numbers) of the number of cells where;
A. Cell 1 = green and cell 2 = green
B. Cell 1 = green and cell 2 = red
c. Cell 1 = red and Cell 2 = green
D. Cell 1 = red and cell 2 = red
I have a column of 800 items to compare and do not want to count each one.
Thanks


2016-08-31 16:38:17

Rudi

For my monthly financial payments I use an Excel with a summary function to know which amounts are debited in which period. When theyy are charged I mark the cell green. Is there a way to run a summary function only on fields with a certain color ? (In this case the ones "White" as the debit is open and I have to make money available for that). Thanks in advance. Rudi


2016-07-26 10:34:18

Eli

You can also use the find and replace to search by format, change the number format of the cells and then use CELL() and "format" to reference it in an IF()


2016-06-29 02:05:05

SAM

if I Want to put Value color in cell
what can i do?
Ex: if a<5 = red
if a>5 = green
if a>10= yellow


2016-02-25 15:22:36

Adam

I got CheckColor1 to work on one target cell. How would one modify the code to check a target range of cells and if one of the cells in the target range matches the defined color, the text appears?


2016-02-23 12:26:41

satyen

if in a raw three cells filled with color R, Y & B randomly. How can I get column / cell no in which particular color is filled


2016-02-01 17:45:19

Venkat

How can I apply color to a cell using a formula or function based on the value in a different cell.
For example, if C1<1%, Apply Green color to A1 and B1 else Apply Red.
I need it in the form of formula or a function instead of conditional formatting as I have to insert this formula into the cell automatically from a script I am writing instead of doing it manually everytime


2016-01-22 11:42:33

Silke Wolf

Hey! I can't seem to wrap my head around this for my spreadsheet. (2013) What I want to do is take a cell with an If statement that will end up equaling a running total. Have that 'If' cell reference a column of cells. If any of those cells are white, then it needs to reference another cell that has a running total it and take that current running total amount and have it subtracted by another cell in the same row as the cell w the color white is found and reflect that total. Am I out of my mind?


2016-01-08 02:22:05

krishna

If cell A1 colour is Greeen , I need to say in Cell B2 "Completed" or "Incompleted", I need to write the conduction for this , kindly help me


2015-12-26 06:09:02

Prasenjit Bera

What I am wanting to do is set up a spreadsheet for my water parameters where for example the PH value entered is between 6.5 and 7.0 it colors the cell green if it is outside those parameters I get a red cell warning me


2015-06-12 01:02:00

joe H

What I am wanting to do is set up a spreadsheet for my water parameters where for example the PH value entered is between 6.5 and 7.0 it colors the cell green if it is outside those parameters I get a red cell warning me


2015-06-10 06:09:45

Sherry

Hi, I wonder if someone can help me, I have a master spreadsheet to track our project status, I can set up conditional formatting so that if a date is less than today it turns red, however I also want the function to ignore certain cells where that task has been completed. These are highlighted with green text. Is there a way I can tell excel in a conditional format to highlight any dates less than today to highlight red except for those already highlighted green? Thanks


2015-03-23 05:54:52

Willy Vanhaelen

@ S V THAKURDESAI

You can do this with conditional formatting. See Excel help or at this site:
http://excelribbon.tips.net/C0787_Conditional_Formatting.html


2015-03-23 02:27:00

S V THAKURDESAI

hallow sir
one of the coloumns of my worksheet contains value which is derived by using some formula. none of the cells of this coloumn is filled with colours. could you suggest a function so that if the all cells of this coloumn containing value which are, say, not between 30 and -30 are filled with, say yellow color?


2015-03-02 12:20:50

paolo

Hi All,

If highlight in yellow one cell included into a range (for example btw. A1:A10), I need that its content (text) will be copied automatically to another desired cell (for example C35). Could you help me how can I set the right function in order to do it? I'm a beginner about VBA. Thank you in advance for your support!
Paolo


2015-02-01 15:37:05

Debbie

I've created the following UDF but cannot apply it across columns when I use =CheckColor1(H13:M13)

Function CheckColor1(range)
If range.Interior.Color = RGB(0, 176, 80) Then
CheckColor1 = "Final"
ElseIf range.Interior.Color = RGB(255, 255, 0) Then
CheckColor1 = "Final"
ElseIf range.Interior.Color = RGB(191, 191, 191) Then
CheckColor1 = "Final"
ElseIf range = 0 Then
CheckColor1 = "Final"
Else
CheckColor1 = " "
End If
End Function


2015-01-15 01:56:50

shakeel akbar

dear
i want the comparison of two cells if its come positive its will be green and if negative it will be red
have any formula for this


2014-09-30 08:52:08

Glenn

Sushanth:

Yes, you can do that by modifying the GetFillColor function. Here's an example:

Function CompFillColor(Rng1 As Range, Rng2 As Range) As Boolean
CompFillColor = (Rng1.Interior.ColorIndex = Rng2.Interior.ColorIndex)
End Function


2014-09-30 04:32:06

Sushanth

Is there any way that we can compare two cells with colors.

For example, if A1 contains Green color and A2 contains Red, then A3 should return false.

Similarly, if A1 contains Green color and A2 also contains Green, then A3 should return false.


2014-06-29 21:35:22

Moe

Dear Alen, thanks for your description and step. what I'm looking for is this, if i have 2 columns, and in first column are specification for a laptop, and in second is the brand name. how is it possible, to use X for Poor, Z for Average and Y for High, meaning if i put Y in Ram, it becomes red because in Appendix Y is red color, if i put X its blue because bla bla bla..


2014-02-25 06:45:57

Jeroen Steinfort

Dear Allen,

First of all, thank you for your brilliant written explanation about the use of colors in an IF function.

Secondly, I am trying to construct a function in Excel that is supposed to act like a type of MATCH() function, checking the cell's interior color instead of values, then returning the relative position of the first cell that matches the search criteria (which id like to be a color).

Is it possible to use the GET.CELL() formula instead of VBA?

I'm looking for something like this:

=MATCH(GET.CELL(63;A1);GET.CELL(63;B1:E1);0)

Thanks in advance!

Regards,

Jeroen


This Site

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.

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.