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

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

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

Excel provides several different functions that you can use to generate random numbers. One of the most useful is the ...

Discover MoreFunctions are at the heart of Excel's power in working with data. One of the most misunderstood functions provided by ...

Discover MoreCells can store all sorts of information that can be formatted and displayed in a myriad of ways. If you want to quickly ...

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

2020-12-05 10:25:49

Thanks

Kashmira

2020-12-03 20:16:43

Christian Quintero

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

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

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.

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

2020-05-14 03:21:31

Stef

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

2020-02-11 17:42:05

2020-02-07 18:37:11

Terry

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

================================================

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

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

2018-05-02 04:04:55

Mostafa`

Thank you so much. this really helped.

2018-03-01 18:37:01

Larry Spiegel

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

Thank you. Way out of my area of knowledge

2017-08-23 18:02:04

Lynndoom

I need to add the dollar amounts filled yellow in the same collumn.

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

## Comments