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

by Allen Wyatt
(last updated June 21, 2018)

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

Understanding the While...Wend Structure

Logical structures are important in programming, as they allow you to control how the programming statements are ...

Discover More

Getting Word to Remember the Default Date and Time Format

One way to insert the current date into your document is to use the Date and Time dialog box. The Default button in the ...

Discover More

Strange ATAN Results

You may use Excel's trigonometric functions to do some quick calculations, and suddenly notice that the results in your ...

Discover More

Program Successfully in Excel! John Walkenbach's name is synonymous with excellence in deciphering complex technical topics. With this comprehensive guide, "Mr. Spreadsheet" shows how to maximize your Excel experience using professional spreadsheet application development tips from his own personal bookshelf. Check out Excel 2013 Power Programming with VBA today!

##### More ExcelTips (ribbon)

Modifying Proper Capitalization

The PROPER worksheet function is used to change the case of text so that the first letter of each word is capitalized. If ...

Discover More

Calculating Combinations

The COMBIN function is used to determine the number of combinations that can be made from a group of elements. This tip ...

Discover More

Median of Selected Numbers

Need to find a median value in a series of values? It's easy with the MEDIAN function. What isn't as easy is to derive ...

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.

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}] 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 2?

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

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)

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.