In the latest version of Excel, it is directly possible to use a specific cell color as the condition when using the IF function. But for previous versions, with a little help from Excel macros, there are some work-arounds allowing you to easily perform this task. In this tutorial, we are going to demonstrate what you can do with a cell value if the cell color is green or any other specific color in Microsoft Excel.
Defining a custom function with the help of the name defining feature will be a required step in all the operations we’ll perform below.
Example 1 – If Cell Color Is Green Then Set Cell Value
Let’s set cell values if the cell color is green in our sample dataset. From the dataset, we can see that our second and fifth entries are green. We want to associate “Absent” with them, and “Present” with the rest.
Let’s create the custom function to do this, then set the cell value if the cell color is green or any other color.
Steps to Define Custom Function:
- Go to the Formulas tab on your ribbon.
- Select Name Manager from the Defined Names.
- The Name Manager box will open up. Now click on New on top of the box.
- In the New Name box, enter a name for the function in the Name field.
- In the Refers to field, enter the following:
=GET.CELL(38,Sheet1!B5)
- Click on OK.
Now we have a formula called GreenCheck ready that can be used for other purposes.
Steps to Set Values:
- To find out the color code of green (or any other color), select cell C5 and enter the following formula:
=GreenCheck
- Press Enter and click and drag the Fill Handle to the end to find values for all.
We can see the color code of green is 50.
- Clear the values you just entered.
- Select cell C5 now and enter the following formula:
=IF(GreenCheck=50,"Absent","Present")
- Press Enter.
- Select the cell again. Click and drag the Fill Handle icon to the end of the list to replicate the formula for the rest of the cells.
Example 2 – If Cell Color Is Green Then Modify Cell Value
Let’s say there are values already associated with the cells, that we now want to modify.
Steps to Define Custom Function:
- Go to the Formulas tab on your ribbon.
- Select Name Manager.
- The Name Manager box will open up. Click on New on top of the box.
- In the New Name box, write down a name for the function in the Name field.
- In the Refers to field, enter the following:
=GET.CELL(38,Sheet1!B5)
- Click on OK.
Now we have the formula called GreenCheck ready that can be used for other purposes.
Steps to Modify Cell Values:
Let’s make a chart similar to the one in the previous Example.
- Select cell C5 and enter the following formula:
=IF(GreenCheck=50,"Absent","Present")
- Press Enter.
- Select the cell again. Now click and drag the Fill Handle icon to the end of the list to replicate the formula for the rest of the cells.
Now let’s modify these values. Let’s say, we want the third entry to be green too.
- Select cell B6.
- Go to the Home tab of your ribbon.
- Select Format Painter from the Clipboard group.
- Click on cell B7.
The value of cell C7 will now automatically change to “Absent”.
Example 3 – If Cell Color Is Green Then Remove Cell Value
Now let’s say there are values already set on the dataset and we want to remove them based on their color. As before, first, we need the custom function.
Steps to Define Custom Function:
- Go to the Formulas tab on your ribbon.
- Select Name Manager from the Defined Names.
- The Name Manager box will open up. Click on New on top of the box.
- In the New Name box, enter a name for the function in the Name field.
- In the Refers to field, enter the following:
=GET.CELL(38,Sheet1!B5)
- Click on OK.
Now we have the formula called GreenCheck ready that can be used for other purposes.
Steps to Remove Cell Values:
Let’s say the dataset has the values shown below.
To remove the values associated with the green cells:
- Select cell C5 and enter the formula:
=IF(GreenCheck=50,"","Present")
- Press Enter.
You may not see any changes yet depending on your dataset.
- Select cell C5 again and click and drag the Fill Handle icon to the end to replicate the formula.
The cell values will be cleared if the cell color of the adjacent cell is green.
Example 4 – If Cell Color Is Green Then Count Cells
Suppose the dataset looks like this:
To count the number of cells filled with green or any color we’ll utilize the COUNTIF function. But first, we need to define the custom function.
Steps to Define Custom Function:
- Go to the Formulas tab on your ribbon.
- Select Name Manager from the Defined Names.
- The Name Manager box will open up. Click on New on top of the box.
- In the New Name box, enter a name for the function in the Name field.
- In the Refers to field, enter the following:
=GET.CELL(38,Sheet1!B5)
- Click on OK.
Now we have the formula called GreenCheck ready that can be used for other purposes.
Steps to Count Cells:
- To count the green cells, select cell C5 and enter the following formula:
=GreenCheck
- Press Enter.
- Select the cell again. Then click and drag the Fill Handle icon to the end of the column to fill the cells with the formula.
Now we have all the color codes of the adjacent cells in the dataset.
- Select cell C12 and enter the following to count the green cells:
=COUNTIF(C5:C10,50)
- Press Enter.
Example 5 – If Cell Color Is Green Then Sum Cell Values
Now let’s assume there are other values associated with the rows of the color-coded cells. For example, let’s look at the following dataset.
The way our color code functions are set up, the color code column has to be on the right of the colored cells.
Let’s use the SUMIF function to sum the values associated with green cells.
Steps to Define Custom Function:
- Go to the Formulas tab on your ribbon.
- Select Name Manager from the Defined Names.
- The Name Manager box will open up. Click on New on top of the box.
- In the New Name box, enter a name for the function in the Name field.
- In the Refers to field, enter the following:
=GET.CELL(38,Sheet1!B5)
- Click on OK.
Now we have the formula called GreenCheck ready that can be used for other purposes.
Steps to Sum Cell Values:
- Select cell C5 and enter the following formula:
=GreenCheck
- Press Enter.
- Select the cell again and click and drag the Fill Handle icon to the end of the list to replicate the formula.
- Select cell C12 and enter the formula:
=SUMIF(C5:C10,50,D5:D10)
- Press Enter.
Download Practice Workbook
Related Articles
- Excel Color Index Formula
- If Cell Color Is Red Then Execute Different Functions in Excel
- VBA to Check If Font Color is Red Then Return Results in Excel
<< Go Back to Excel Get Cell Color | Excel Cell Format | Learn Excel
Get FREE Advanced Excel Exercises with Solutions!