# Colors in an IF Function

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: Colors in an IF Function.

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 = 14 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)=14,"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 (10779) 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: Colors in an IF Function.

Related Tips:

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!

 *Name: Email: Notify me about new comments ONLY FOR THIS TIP Notify me about new comments ANYWHERE ON THIS SITE Hide my email address *Text: *What is 5+3 (To prevent automated submissions and spam.)

Sudhakar    17 Apr 2016, 13:21
I HAVE TO DEFINE CELL COLOR IF ADJACENT CELL IS 1,2,3.....10. IF ABOVE THAN 10 SHOULD BE "NO COLOR".

WHAT SHOULD BE THE FORMULA FOR THIS
Linda Jashari    08 Jan 2016, 10:25
I am having problems with this step:
"2.Click Insert | Name | Define. Excel displays the Define Name"

after I select cell A1 (do I right click to "insert" if so, I do not get those options. Please help.

Can you please respond to me?
Randy    26 Jan 2012, 00:56
For formulas involving cell formats, such as color in this case, I use a simple non-macro workaround that usually works for me:

1. Copy the target cells containing the format criteria to a new column.
2. Select the data in the new column.
3. Do a search/replace (Ctrl-H, click "Options" if necessary, to reveal format options) to replace cells meeting the format criteria with an "XXYYZZ" or some other tag unique from the rest of the data.
3. Apply your formula to the tag, basically replacing the format condition.
4. If desired, hide the new column.

# Our Company

Sharon Parq Associates, Inc.

# Our Sites

Tips.Net

Beauty and Style

Cars

Cleaning

Cooking

ExcelTips (Excel 97–2003)

ExcelTips (Excel 2007–2016)

Gardening

Health

Home Improvement

Money and Finances

Organizing

Pests and Bugs

Pets and Animals

WindowsTips (Microsoft Windows)

WordTips (Word 97–2003)

WordTips (Word 2007–2016)

Excel Products

Word Products

# Our Authors

Author Index

Write for Tips.Net