In this article, you will learn new effective approaches to summing and counting cells in Excel by color. These solutions work for cells colored manually and with conditional formatting in all versions of Excel 2010 through Excel 365.
Even though Microsoft Excel has a variety of functions for different purposes, none can calculate cells based on their color. Aside from third-party tools, there is only one efficient solution - create your own functions. If you know very little about user-defined functions or have never heard of this term before, don't panic. The functions are already written and tested by us. All you need to do is to insert them in your workbook :)
Below, you can see the codes of two custom functions (technically, these are called user-defined functions or UDF). The first one is purposed for counting cells with a specific fill color and the other - font color. Both are written by Alex, one of our best Excel gurus.
Once the functions are added to your workbook, they will do all work behind the scenes, and you can use them in the usual way, just like any other native Excel function. From the end-user perspective, the functions have the following look.
To count cells with a particular background color, this is the function to use:
To count cells of a specific color in a given range, carry out these steps:
For example, to find out how many cells in range B3:F24 have the same color as H3, the formula is:
In our sample dataset, the cells with values less than 150 are colored in yellow, and the cells with values higher than 350 in green. The function gets both counts with ease:
In case your cell values have different font colors, you can count them using this function:
For example, to get the number of cells in B3:F24 whose values have the same font color as H3, the formula is:
Tip. If you'd like to name the functions differently, feel free to change the names directly in the code.
To sum colored values, add the following two functions to your workbook. As with the previous example, the first one handles fill color and the other - font color.
To sum by fill color in Excel, this is function to use:
For example, to add up the values of all cells in B3:F24 that are shaded with the same color as H3, the formula is:
To sum numeric values with a specific font color, use this function:
For instance, to add up all the values in cells B3:F24 with the same font color as the value in H3, the formula is:
To count and sum cells of a certain color in all sheets of a given workbook, we created two separate functions, which are named WbkCountByColor and WbkSumByColor, respectively. Here comes the code:
Note. To make the functions' code more compact, we refer to the two previously discussed functions that count and sum within a specified range. So, for the "workbook functions" to work, be sure to add the code of the CountCellsByColor and SumCellsByColor functions to your Excel too.
To find out how many cells of a particular color there are in all sheets of a given workbook, use this function:
The function takes just one argument - a reference to any cell filled with the color of interest. So, a real-life formula may look something like this:
Where A1 is the cell with the sample fill color.
To get a total of values in all cells of the current workbook highlighted with a particular color, use this function:
Assuming the target color is in cell B1, the formula takes this form:
The custom functions for adding up and counting color-coded cells are really nice, aren't they? The problem is that they do not work for cells colored with conditional formatting, alas :(
To handle conditional formatting, we have written a different code (kudos to Alex again!). It works well with both preset formats and custom formula-based rules. Contrasting with the previous examples, this code is a macro, not a function. The macro counts and sums conditionally formatted cells by fill color. Please insert it in your VBA Editor, and then follow the below instructions.
With the macro's code inserted in your Excel, this is what you need to do:
For this example, we used the inbuilt Highlight Cell Rules and got the following results:
Tip. The sample workbook with the SumCountByConditionalFormat macro is available for download at the end of this post.
If you need (or are curious) to know the color of a specific cell (fill or font color), add the following user-defined functions to your Excel. It returns ColorIndex as a decimal number.
Note. The functions only work for colors applied manually, and not with conditional formatting.
To return a decimal code of the color a given cell is highlighted with, make use of this function:
For example, to get the color of cell A2, the formula is:
To get a font color of a cell, use an analogous function:
For instance, to find the font color of cell E2, the formula is:
To convert a decimal color index returned by our custom functions into a hexadecimal color code, make use of Excel's native DEC2HEX function.
To add the function's or macro's code to your Excel, move on with these 4 steps:
If you are not very comfortable with VBA, you can find the detailed step-by-step instructions and a handful of useful tips in this tutorial: How to insert and run VBA code in Excel.
When summing and counting color-coded cells in Excel, please keep in mind that your formulas won't recalculate automatically after coloring a few more cells or changing existing colors. Please don't be angry with us, this is not a bug in our code :)
The point is that changing cell color in Excel does not trigger worksheet recalculation. To get the formulas to update, press either F9 to recalculate all open workbooks or Shift + F9 to recalculate only the active sheet. Or just place the cursor into any cell and press F2, and then hit Enter. For more information, please see How to force recalculation in Excel.
If you do not want to waste time tinkering with VBA codes, I'm happy to introduce you to our very simple but powerful Count & Sum by Color tool. Together with 70+ other time-saving add-ins, it is included with Ultimate Suite for Excel.
Once installed, you will find it on the Ablebits Tools tab of your Excel ribbon:
And here is a short summary of what the Count & Sum by Color add-in can do:
Selecting the Sum & Count by One Color option will open the following pane in the left part of your worksheet. You specify the source range and sample cell, then then click Calculate.
The result will appear on the pane straight away! No macros, no formulas, no pain :)
Apart from count and sum, the add-in also shows Average, Max and Min for colored numbers. To insert a particular value in the sheet, click the Paste button next to it. Or click Paste All to have all the results inserted at once:
To handle all colored cells at a time, choose the Sum & Count by All Color option. Basically, it works in the same way, except that instead of color, you choose the function to calculate.
Tip. To have the results inserted in the worksheet as formulas (custom functions), check the corresponding box at the bottom of the pane.
Well, calculating colored cells in Excel is pretty easy, isn't it? Of course, if you have that little gem that makes the magic happen :) Curious to see how our add-in will cope with your colored cells? The download link is right below.
Table of contents