You might have a colorful dataset and you would like to use the cell color to work with the Excel formula. Excel has a lot of amazing bunches of formulas to write and read data from datasets. Some of them are COUNT, SUBTOTAL, IF, and so on. Again, you can also use VBA macros to build new formulas according to requirements which you can apply for different cell colors. This article will explain 5 examples of Excel Formula based on cell color with proper illustrations. Show
Download Practice WorkbookYou can download the practice workbook from here. 5 Examples of Excel Formula Based on Cell ColorWe will use the following colorful dataset to explain the methods. We can see that the dataset has two columns namely Name and Quantity. There are 3 different colors in the rows. We will be applying different Excel formulas like SUMIF, SUBTOTAL, IF, and user-defined functions using VBA macros in the 5 examples coming next. So, without any delay, let us jump into the main discussion. 1. Excel SUBTOTAL Formula with Cell ColorTo apply the Excel formula SUBTOTAL to count and get the sum of values filtered by color. Follow the steps below for this method. Steps:
We can also use a SUBTOTAL formula for summation purposes. Let us see.
You will find two arrows in the columns of the dataset.
It will show the filtered dataset. You can notice the changes of values in Count by Color and Sum by Color in the pictures below. The results show the count and sum of only the filtered data 🔎How Does the Formula Work? 📌 SUBTOTAL takes two arguments function_name and ref1.In the function_name it takes 102 for counting the number of data and 109 to return the sum of the quantities. 📌 As reference both the formulas take a range of the quantities. 📌 The result at the start shows all the data in the range. However, the last two pictures show the result of filtered cells only. Read More: Excel Cell Color: Add, Edit, Use & Remove 2. Excel COUNTIF and SUMIF Formula by Cell Color2.1 COUNTIF Formula with Cell ColorNow, if you want to apply the COUNTIF formula by cell color you need to follow the steps below. Steps:
It will show in the Name Manager box.
You will get the code for all the colors present in the dataset.
In Cell G6, In Cell G7, You will see the result as shown in the pictures above. Anyway, you can also write the mixed or, relative cell reference of each of the cells in the formula and simply drag it down to get the results. 2.2 SUMIF Formula with Cell ColorSteps: Type the following formula in Cell H5:
Similarly in Cell H6,
And, in Cell H7,
Observe the pictures above to see how the results are found. 🔎How Does the Process with Formulas Work? 📌 Here, the formula using the GET.CELL function takes 38 to return code color and cell reference of which the code it will return. 📌 By defining Name for with the GET.CELL formula we can simply write the name “NumberColor” prefixed by an equal sign will get the code of colors of the referenced cell. 📌 Next, using the Color codes we have applied the COUNTIF and the SUMIF formula to get the count and sum of data range with color code criteria. Read More:How to Change Cell Color Based on a Value in Excel (5 Ways) 3. Excel IF Formula by Cell ColorNow, let us say we have the same price per piece for products like hoodies, jackets, and sweaters. If you want to calculate the total price for the total quantities of these products, we can use the IF formula. You can follow the steps to apply IF here. Steps:
You can notice that it showed values only for the products with the same color having color code 40 while zero (0) for the rest. 🔎How Does the Formula Work? 📌 Here the IF formula takes NumberColor to be equal to 40. 📌 If the logic is true, it will multiply the quantity with the price per piece (5). Otherwise, it will show 0. Read More: How to Highlight Cell Using the If Statement in Excel (7 Ways) Similar Readings
4. Excel SUMIFS Formula by Cell ColorUsing Color code, we can also apply the SUMIFS formula. For that, you need to follow the steps below: Steps:
🔎How Does the Formula Work? 📌 The SUMIFS formula takes the sum_range C5:C10 as absolute references for quantities. Followingly, it takes the color code range which is also in absolute reference form. 📌 Lastly, the criteria is set for the first cell of the color code column which is D5. In this case, only the column is in absolute reference form while the rows are in relative reference form. It is because it will drag the fill handle icon for the rest of the column by changing the row numbers as required. Related Content: How to Highlight Cells in Excel Based on Value (9 Methods) 5. Excel VBA Macro to Excel Formula by Cell ColorMoreover, VBA Macro can be an amazing tool to apply excel formulas by cell color. Let us subdivide this method into two parts for the convenience of understanding. The first sub-method will use the code to find the color code and then apply them to apply the COUNTIF and the SUMIF formulas Note: VBA Macro cannot recognize similar colors and so we modified our dataset with differentiable colors. The three different colors are red, blue, and brown. Now let us see how we can use VBA Macro to apply Excel formula by cell color. 5.1 VBA Macro to Find Color CodeTo find the color code using VBA Macro and apply the Excel formulas, we have to follow the steps below. Steps:
Code:
For this case, you have to find out the sum using color code. However, you can directly do the sum by writing a code. This will be explained in the next sub-method. 🔎How Does the Process with Formulas Work? 📌 We have created ColorIndex using the code and keeping the argument as the range of the data. Using this we get the color codes. 📌 Next, we used the COUNTIF formula to get the count result for that particular color code. 📌 Lastly, we used the SUMIF formula to get the sum based on the color code. 5.2 VBA Macro to SumYou have to follow the following steps to get the summation of the quantities of the same color directly through code. Steps:
Code:
You will get the result as shown in the above picture. 🔎How Does the Process with Formulas Work? 📌 We created a formula with the name SBC through the code we have written in the General window for this worksheet. 📌 After that, we used the formula with a range of data and criteria as the particular cell of quantities. Read More:VBA to Change Cell Color Based on Value in Excel (3 Easy Examples) Things to Remember1. You have to use different colors in case of applying VBA Macro. 2. You have to save the Excel file with the .xlsm suffix in case of the file has VBA Macro codes within it. ConclusionThe article explains 5 different methods to apply Excel formulas like SUMIF, SUBTOTAL, COUNTIF, and so forth based on cell color. Moreover, the practice workbook is there for you, so you can download it, and apply any of the methods as per your requirement. For any further queries, please write in the comment section. Related Articles
How do I make an if formula in Excel with color?You can color-code your formulas using Excel's conditional formatting tool as follows. Select a single cell (such as cell A1). From the Home tab, select Conditional Formatting, New Rule, and in the resulting New Formatting Rule dialog box, select Use a formula to determine which cells to format.
Can you write Excel formula based on cell color?Select the cells with the background color. Hit Alt + F8 and select the macro that we just added and click the Run button. Now, you will have a column of numbers that represents each color. Once you have these values for the numbers, you can then use them in any conditional function or formula.
How do you add color to an IF statement?When the value is greater than or equal to 75% , return Red . If the value is less than 75% , return Green . Then we can create a measure to return colors . Then set conditional formatting for [Value] .
...
The Power BI Community Show.. Can you count if based on cell color?The COUNT function in Excel counts cells containing numbers in Excel. You cannot count colored or highlighted cells with the COUNT function.
|