**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.

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:

- Select cell A1.
- Click Insert | Name | Define. Excel displays the Define Name dialog box.
- Use a name such as "mycolor" (without the quote marks).
- In the Refers To box, enter the following, as a single line:
- Click OK.

=IF(GET.CELL(38,Sheet1!A1)=10,"GO",IF(GET.CELL(38,Sheet1!A1) =3,"Stop","Neither"))

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**.

**Solve Real Business Problems** Master business modeling and analysis techniques with Excel and transform data into bottom-line results. This hands-on, scenario-focused guide shows you how to use the latest Excel tools to integrate data from multiple tables. Check out *Microsoft Excel 2013 Data Analysis and Business Modeling* today!

Indirect references can be very helpful in formulas, but getting your head around how they work can sometimes be confusing. ...

Discover MoreExcel allows you to easily format cells with different fonts, borders, and colors. If you want to count the number of cells ...

Discover MoreGot a ton of names from which you need to select a few random names? There are several ways you can extract what you need; ...

Discover More**FREE SERVICE:** Get tips like this every week in *ExcelTips,* a free productivity newsletter. Enter your address and click "Subscribe."

2017-06-19 14:56:32

Barrett

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

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

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

2016-07-26 10:34:18

Eli

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

2016-02-23 12:26:41

satyen

2016-02-01 17:45:19

Venkat

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

2016-01-08 02:22:05

krishna

2015-12-26 06:09:02

Prasenjit Bera

2015-06-12 01:02:00

joe H

2015-06-10 06:09:45

Sherry

2015-03-23 05:54:52

Willy Vanhaelen

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

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

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

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

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

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

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

2014-02-25 06:45:57

Jeroen Steinfort

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

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 © 2017 Sharon Parq Associates, Inc.

## Comments