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 July 20, 2015)

22

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

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

Changing Directories in a Macro

When a macro works with files, it often has to change between different directories on your disk drive. This is done using ...

Discover More

Edits Cause Text to Switch to Odd Fonts

If you have problems with strange fonts showing up when you paste information into a document, it is helpful to understand ...

Discover More

Calculating a Date Five Days before the First Business Day

Excel allows you to perform all sorts of calculations using dates. A good example of this is using a formula to figure out a ...

Discover More

Comprehensive VBA Guide Visual Basic for Applications (VBA) is the language used for writing macros in all Office programs. This complete guide shows both professionals and novices how to master VBA in order to customize the entire Office suite for their needs. Check out Mastering VBA for Office 2010 today!

More ExcelTips (ribbon)

Converting to Hexadecimal

Excel allows you to easily convert values from decimal to other numbering systems, such as hexadecimal. This tip explains how ...

Discover More

Making VLOOKUP Case Sensitive

The VLOOKUP function, like other lookup functions in Excel, is not case sensitive. In other words, it doesn't matter whether ...

Discover More

Using the XIRR Function

One of the financial worksheet functions provided in Excel is the XIRR function. This is used to figure out an internal rate ...

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}] in your comment text. You’ll be prompted to upload your image when you submit the comment. 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 6 + 4?

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.