-
— By
Sumit Bansal
Watch Video – How to Count Colored Cells in Excel
Wouldn’t it be great if there was a function that could count colored cells in Excel?
Sadly, there isn’t any inbuilt function to do this.
BUT..
It can easily be done.
How to Count Colored Cells in Excel
In this tutorial, I will show you three ways to count colored cells in Excel (with and without VBA):
- Using Filter and SUBTOTAL function
- Using GET.CELL function
- Using a Custom Function created using VBA
#1 Count Colored Cells Using Filter and SUBTOTAL
To count colored cells in Excel, you need to use the following two steps:
- Filter colored cells
- Use the SUBTOTAL function to count colored cells that are visible (after filtering).
Suppose you have a dataset as shown below:
There are two background colors used in this data set (green and orange).
Here are the steps count colored cells in Excel:
- In any cell below the data set, use the following formula: =SUBTOTAL(102,E1:E20)
- Select the headers.
- Go to Data –> Sort and Filter –> Filter. This will apply a filter to all the headers.
- Click on any of the filter drop-downs.
- Go to ‘Filter by Color’ and select the color. In the above dataset, since there are two colors used for highlighting the cells, the filter shows two colors to filter these cells.
As soon as you filter the cells, you will notice that the value in the SUBTOTAL function changes and returns only the number of cells that are visible after filtering.
How does this work?
The SUBTOTAL function uses 102 as the first argument, which is used to count visible cells (hidden rows are not counted) in the specified range.
If the data if not filtered it returns 19, but if it is filtered, then it only returns the count of the visible cells.
Try it Yourself.. Download the Example File
#2 Count Colored Cells Using GET.CELL Function
GET.CELL is a Macro4 function that has been kept due to compatibility reasons.
It does not work if used as regular functions in the worksheet.
However, it works in Excel named ranges.
See Also: Know more about GET.CELL function.
Here are the three steps to use GET.CELL to count colored cells in Excel:
- Create a Named Range using GET.CELL function
- Use the Named Range to get color code in a column
- Using the Color Number to Count the number of Colored Cells (by color)
Let’s deep dive and see what to do in each of the three mentioned steps.
Creating a Named Range
Getting the Color Code for Each Cell
In the cell adjacent to the data, use the formula =GetColor
This formula would return 0 if there is NO background color in a cell and would return a specific number if there is a background color.
This number is specific to a color, so all the cells with the same background color get the same number.
Count Colored Cells using the Color Code
If you follow the above process, you would have a column with numbers corresponding to the background color in it.
To get the count of a specific color:
- Somewhere below the dataset, give the same background color to a cell that you want to count. Make sure you are doing this in the same column that you used in creating the named range. For example, I used Column A, and hence I will use the cells in column ‘A’ only.
- In the adjacent cell, use the following formula:
=COUNTIF($F$2:$F$20,GetColor)
This formula will give you the count of all the cells with the specified background color.
How Does It Work?
The COUNTIF function uses the named range (GetColor) as the criteria. The named range in the formula refers to the adjacent cell on the left (in column A) and returns the color code for that cell. Hence, this color code number is the criteria.
The COUNTIF function uses the range ($F$2:$F$18) which holds the color code numbers of all the cells and returns the count based on the criteria number.
Try it Yourself.. Download the Example File
#3 Count Colored Using VBA (by Creating a Custom Function)
In the above two methods, you learned how to count colored cells without using VBA.
But, if you are fine with using VBA, this is the easiest of the three methods.
Using VBA, we would create a custom function, that would work like a COUNTIF function and return the count of cells with the specific background color.
Here is the code:
'Code created by Sumit Bansal from https://trumpexcel.com
Function GetColorCount(CountRange As Range, CountColor As Range)
Dim CountColorValue As Integer
Dim TotalCount As Integer
CountColorValue = CountColor.Interior.ColorIndex
Set rCell = CountRange
For Each rCell In CountRange
If rCell.Interior.ColorIndex = CountColorValue Then
TotalCount = TotalCount + 1
End If
Next rCell
GetColorCount = TotalCount
End Function
To create this custom function:
To use this function, simply use it as any regular excel function.
Syntax: =GetColorCount(CountRange, CountColor)
- CountRange: the range in which you want to count the cells with the specified background color.
- CountColor: the color for which you want to count the cells.
To use this formula, use the same background color (that you want to count) in a cell and use the formula. CountColor argument would be the same cell where you are entering the formula (as shown below):
Note: Since there is a code in the workbook, save it with a .xls or .xlsm extension.
Try it Yourself.. Download the Example File
Do you know any other way to count colored cells in Excel?
If yes, do share it with me by leaving a comment.
You May Also Like the Following Excel Tutorials:
- Count Cells that Contain Text
- How to Sum by Color in Excel (Formula & VBA)
- Filter Cells with Bold Font Formatting
- How to Format Partial Text Strings using VBA
- Highlight EVERY Other ROW in Excel (using conditional formatting).
- How to Quickly Highlight Blank Cells in Excel.
- How to Compare Two Columns in Excel.
Get 51 Excel Tips Ebook to skyrocket your productivity and get work done faster
67 thoughts on “How to Count Colored Cells in Excel – A Step by Step Tutorial + Video”
-
I’m noticing that the VBA method doesn’t work with conditional formatting of background colors. Any workaround for hat?
-
I have the same issue
-
-
Hi, I am using the VBA module to count preferred meeting session times, it was working OK – until I added a separate VBA module called CounxlDiagonalDown to count diagonal borders (people who have indicated they are not attending said meeting), and now I am getting a #NAME? syntax error on the GetColorCount. any suggestions on a fix? Thanks
-
Thanks, your VB macro works perfectly!!
-
Great stuff, but the VB does not work on conditional formatting.. only if you manually change the background color of cells. Have any idea to count backgrounds with conditional formating?
-
Thank you so much. Just what I needed.
-
Great vba function! Very useful. I change the ‘interior’ to ‘font’ and it works nicely when counting range with different font colors. However, I tried it to conditional formatting and it didn’t work. Any idea, why?
-
Does this work with horizontal range. Doesnt seem to be working in mine with an horizontal range
-
Fantastic video and article. Very helpful and simple to follow. Thank you!
-
Simple and straight forward. This did the job. Thank you!
-
I have a set of two ranges with compassion:
Say my A2:A10 with data of
B2:B10 with data of
I applied function in C2 and draged upto C10<=if(isna(Match($A$2:$A$10,$B$2:$B$10,0)),A2,» «)
Which give me the result as (this result says not shared number of Set A2:A10 to B2:B10)
Now I coloured Full set of A2:A10 and also B4 and B9 as yellow and I need the out put in C2 to C10 asCan any body help me to fix this problem
-
(VBA Solution)
Too good to be true…just throws the standard formula error (“not trying to type a formula etc.” -
Thank you so much, this solve my problems.
-
The code in the screen shot of the VBA has errors
-
Help! Is there anyway to modify this VBA so that it works for =max
-
GOOD TRICKS FOR COUNTING BY COLOUR
-
tried =SUBTOTAL(102,$E$2:$E$20) and didn’t work. waste of time
-
Is there a way to count the cells by color but also having a criteria?. For example I want to count the cells in a column that are colored grey but only the ones that have an specific value on them like having the word “unique” in the cell.
I would really appreciate the help thanks
-
I know this is kind of a ghetto solution, but:
I just copied the unique cells I needed to a different worksheet and did the function there, it worked perfectly
-
-
Great code. However, it does not automatically update, unless I click on the cell each time. Please note, I have excel set to automatically update calculations, so not sure why not working.
-
The VBA function worked great!! I’m now showing off to anyone in the office who will listen! Thanks very much
-
Hello! I cannot get my get.cell (38,…) to work. Is there any chance I could contact you?
-
Can you please try with ; instead of , ? It work for me.
-
-
Using VBA was perfect! Thanks
-
I’m using your VBA method, and it works just fine, but is there any way to make it work for cells that have RGB color values instead of the standard ones from the color palette? Using .ColorIndex only works for those types of colors
-
Hi,
The function option works fine, there is only one thing. As soon as one of the colours changes the sum won’t refresh, is there an way to sort this out ?-
Delete the module > save > close and reopen file > add the module again > click on any cell with the formula > press “enter”
-
Push F2.
Push Enter.
-
-
Is there a way to apply this to rows rather than columns?!
-
The VBA formula worked great on my spreadsheet. But, I saved it and re-opened and now all the cells where I had formulas say #Name?. I checked and the code is still in the module. It comes up in the list when I begin to type =getcolorcount. I tried removing the Code and re-inserting and can’t get the formula to work any longer. I’m using Excel 2010, is there any reason this suddenly wouldn’t work now?
Please help – this formula was perfect for my spreadsheet for work because I didn’t want to have to add another column to the spreadsheet to get this to work.
Thanks,
Jana -
Does this work in an online excel sheet?
-
Hi,
Thanks for the VBA code, I used your code and it works perfect – it counts the colored cells, but it does not update automatically. If a new cell is colored, everyctime I have to click on the formula to have it update the count.
Is there any other way to get this automated when a new cell is colored?
Appreciate your response -
I’ve used your VB method and it works great, thank you so much! I do have an issue though that I’m having troubles resolving…
I’m using your formula as part of a calendar to track equipment utilization by filling the cells with color when the equipment is used. The problem I’m having is that the formula does not automatically recalculate when new colored cells are entered. It does recalculate when you click on the cell containing the formula and press enter, but I’ve got hundreds of assets that I’m tracking and that’s not an efficient way of doing it.
I’ve tried all the usual, F9, recalculate formula, recalculate worksheet, etc. nothing works. I’ve even recorded Macros of actually highlighting all formula cells and clicking enter. It works when I do it, but the macro returns a value error when used.
Do you have a work around for this or another VB Macro that can be assigned to a radio button to recalculate the colored cell totals each time the calendar is updated?
-
Ctrl-Alt-F9
-
-
I found this article very useful, yet the VBA functions didn’t work. I have a table with various data, I used the conditional formatting in one column and built 5 rules and based on that cells have been colored in greed, red and yellow…any thoughts?
-
I have come across 5 functions that count coloured cells such as this code, and none of them work on conditional formatting.
-
-
I have used your following code and it works perfectly:
Function GetColorCount(CountRange As Range, CountColor As Range)
Dim CountColorValue As Integer
Dim TotalCount As Integer
CountColorValue = CountColor.Interior.ColorIndex
Set rCell = CountRange
For Each rCell In CountRange
If rCell.Interior.ColorIndex = CountColorValue Then
TotalCount = TotalCount + 1
End If
Next rCell
GetColorCount = TotalCount
End FunctionBut, now I want to do a little more. For the range of cells, I want to have 4 separate functions. 1) Count cells IF they are a particular color, as well as ending in “*o” 2) Count cells IF they are a particular color, as well as ending in “*s” 3) Count cells IF they are a particular color, as well as having 2 text characters in the cell, “??” and 4) Count cells IF they are a particular color, as well as having a number greater than zero in the cell, “>0”. How can I modify the base “GetColorCount” code to incorporate this additional parameter for each instance?
-
I tried using your 3rd option but I am thinking that I am not right in doing so. Here is what I have and what I am trying to do. I have a spreadsheet with my supervisors and their clock-in times that I export from our schedule and then copy & paste into workbook. I color each row based on if they were on time, late but the time rolled back (7 minute grace period) and late but jumped forward 1/4 hour. I then filter by the supervisor’s last name and then by the “ErrorLog” header, checking only CLOCK IN & LATE CLOCK IN. The “Clock In” option could be either green (on time) or orange (late but rolled back) and then Late Clock In is yellow. The entire ErrorLog column is K10:K118, for all supervisors. Obviously when I sort by both Last Name and ErrorLog headers it reduces the number of rows and hides all the rest. I just want the formula to count each color that is visible. Is what I am doing even possible? I want to be able to change up the filter a bit by changing the Last Name so that I can only see each supervisor individually.
-
Great video…. I do have a question for you… I used your code and it works perfect except for one thing, it counts the colored cells, but it does not update automatically. If a new cell is colored, I have to click on the formula to have it update the count. Is there a code I can add to have it update automatically once a new colored cell is added? Thanks again for a great video!!!
-
Your VBA solution works BUT NOT with colors from “Conditional Formating.”
I have 17 cells in a column, all under a conditional formatting to turn the cell color “light red” if a certain condition is met. There are only 3 cells that are “light red” (meeting the condition) but your VBA script returns an answer of “17”, meaning it considers all cells “light red”. Then I manually went in and colored one cell (not already highlighted by the conditional formatting) blue, and your VBA returned an answer of “16”. Clearly then, it does not recognize the results of conditional formatting, only “manually entered” colors.
Any solution? This is critical as the colored cells will be different depending on what conditions are met. I need a way to count them per each condition.
(I learned a lot about adding a custom VBA code. Thanks!)
-
Same issue! Any suggestions?
-
Was there any answer to this question? thanks.
-
-
Hi,
can you use both GetColorCount and Sumbycolor VBA in the same worksheet. -
Can you please explain what is 38 mentioned in the name manager formula? What does it relate to?
-
If you follow the link to more information about Get.Cell it tells you, I was wondering the same thing.
-
-
When i press ‘enter’ with you’r download exel i get #NAME? why? PS ; I am new in VBA
-
-
-
Hi, please assist. if the cell is in cf it doesn’t count. i am using also the standard cf for date occuring.
-
Hey Mart.. Yes this doesn’t work when conditional formatting is in play. IF you want to count based on CF rules, you can use the same rule you have used to apply conditional formatting and count the total values. For example, if you use CF to highlight cells that contain “Yes”, then use a COUNTIF function to count all these cells.
-
-
How can you accomplish the same thing except counting cells in a row versus column (as outlined here)? For example, I want to know how many of each color (Red, Yellow, Green, and no color) are in row 2 (range – A2:I2).
Side note/issue: The Subtotal function seems to only count cells that have something in them. I would like to count cells that are just a color without any text in the cell. Is that possible?
Thanks for your help.-
I am looking for this answer as well. I need the count of cells from a bigger range a2:bz52. And some cells are merged. Is there a way for this? Thank you in advance! Zita
-
-
I liked the VBA option, works like a charm. However if my data range is non-continuous , for example, instead of A1:A10, I need to count the cell color in A1, A4, A9, How should I modify the formula? Thanks.
-
Hi Azz,
Enclose your non-continuous range within brackets inside the function, (x, y, z, a:c), so for your example, you would use:=GetColorCount( ( A1, A4, A9 ), 50 )
Where,
50 = Green box color index.
-
-
If declared strictly and with names more appealing (IMO ;)). Thank you for the code and the idea behind
Public Function GetColorCount(ByRef Target As Excel.Range, ByRef rgColor As Excel.Range) As Long
Dim rCell As Excel.Range
Dim Color As Long
Dim lgCounter As LongColor = rgColor.Interior.ColorIndex
For Each rCell In Target
If rCell.Interior.ColorIndex = Color Then lgCounter = lgCounter + 1
Next rCell
GetColorCount = lgCounter
End Function -
Hi,
I started using your Count Cells Based on Background Color in Excel #3 using VBA. This wors very good. Thanks for it.
But now I have a problem using the same function by checking cells where the background color is set by Conditional Formatting. I am checking for dates older than today() and mark these cells in red background; works well using conditional formatting but the count doesn’t work for these cells.
thank you for any idea or hint-
Hi Nico,
Do you use a formula in your conditional formatting (CF)?
If you do, it returns TRUE and you are coloring the cells that meet that condition. This means that if you put the CF formula into a COUNTIF formula, you can count the cells that meet the CF conditions!Gr,
Raymond.-
Hi Raymond,
No I’m not using a formula. I use a standard rule type: Cell Value “less than or equal to” =$A$1
In A+ I only update the day =today().
thx
Nico-
Nico,
I did a test and I realised I needed a help column to do the job.
Say that range A2:A10 has your values where you apply the CF to.
Formula in help column B:
in B2 put ‘=OR(A2<$A$1,A2=$A$1)’, copy down and you will get a couple of TRUEs or FALSEs.The count formula anywhere can then be: =COUNTIF(B2:B10,TRUE).
Or you can just use an IF formula in the help column, giving you 1 or 0 back. Then a simple SUM somewhere and there you are.
I must be do-able without the help column but I don’t know how (yet)!
Raymond
-
Hi Raymond,
A help column isn’t very usefull and would need many additional help columns, so I searched and searched.
On http://www.excel-inside.de (a german Excel & VBA site) I found an example using .Font.ColorIndex . With this information I found .Interior.ColorIndex and my day was made 🙂Next step: searching for the color indexes on https://msdn.microsoft.com/en-us/library/office/ff840443.aspx
and it seems to work – refreshing manually after changes – but it works:
‘Function ColorRed(Area As Range)
‘ColorRed = 0
‘For Each cell In Area
‘If cell.Interior.ColorIndex = 3 Then
‘ColorRed = ColorRed + cell.Count
‘End If
‘Next
‘End Function
Nico -
Hi Nico,
Thanks for the update and also for you providing the resources that led to the solution you found!
An UDF (User Defined Function) is harmless in your case because you are using it in only one cell to get the count. So that’s great.
In complex workbooks, where formulas must be in several rows, UDF’s are not recommanded unless no other choice because they make the workbook slow.
And as they say, there is always another way, a simple, faster and stronger way…I found a non-vba solution: an array formula.
Put the following formula adjusted to your needs where you want your count.
In this example, I assume the IF statement is the same as in your conditional format that gives the cells a red color when TRUE. The formula says:
for each cell in range A2:A10, if the cell value is equal or less than the value of A1, give 1, otherwise give 0. Then give a sum of all the individual results.
I’ve used 1 IF with an OR but it didn’t work so it became 2 IF’s.Instead of just pressing ENTER, press CTRL+SHIFT+ENTER to make it an array formula, which you can recognize by the { } that Excel automatically puts around it:
=SUM(IF(A2:A10<$A$1,1,IF(A2:A10=$A$1,1,0)))So after CTRL+SHIFT+ENTER, you will see:
{=SUM(IF(A2:A10<$A$1,1,IF(A2:A10=$A$1,1,0)))}No more manual refresh!
More about the topic:
http://www.cpearson.com/excel/arrayformulas.aspxGr,
Raymond. -
i am not a vbe user but this explanation was clear enough to take the dive;
so i tried solution number 3 and it works!
however, how can i make the sheet update the numbers after i changed the color of a cell? at this moment i need to go to this cell en hit enter;
there must be a ‘trick’ to do this on my imac! -
Hi Cezi,
The only workaround I could came up with involves 2 steps:
[1] Put ‘Application.Volatile True’ at the beginning of the the function code
[2] Put the following code in the worksheet event area (it assumes that you want an update if you select any cell in range [A1:A10]; please adjust accordingly):Private Sub Worksheet_SelectionChange(ByVal Target As Range)
‘Application.StatusBar = False
On Error Resume Next ‘To avoid error if the selection isn’t in [A1:A10]
If Target.Address = Intersect(Target, Range(“A1:A10”)).Address Then
ActiveSheet.Calculate
‘Application.StatusBar = “Calculated”
End If
End SubGr,
Raymond. -
Hello Raymond, I am having the same issue but am not exactly sure how to follow the workaround you posted.
[1] Should I paste ‘Application.Volatile True’ after the following row?
‘Function GetColorCount(CountRange As Range, CountColor As Range)’
[2] Where should I put the code you have entered? I’m afraid I don’t know what the “worksheet event area” is or where I can find it.
Thank you for your patience and a great guide!
/Kara
-
-
Raymond,
Your code for the workaround however disables copying and pasting? Do you know how to fix this?
-Amy
-
Hi Amy,
Sorry about that. Yes, there is a fix. The first line of code should be:
If Application.CutCopyMode Then Exit SubSo, the final worksheet ‘SelectionChange’ event should look like this:
[CODE]
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
‘If we are copying, do nothing and exit
If Application.CutCopyMode Then Exit SubOn Error Resume Next ‘To avoid error if the selection isn’t in [A1:A10]
If Target.Address = Intersect(Target, Range(“A1:A10”)).Address Then
ActiveSheet.Calculate
End If
End Sub
[END CODE]-Raymond
-
-
-
-
Comments are closed.
Top 3 Methods to Count Colored Cells In Excel
There is no built-in function to count colored cells in Excel, but below mentioned are three different methods to do this task.
- Count colored cells by using the Auto Filter option
- Count colored cells by using the VBA code
- Count colored cells by using the FIND method
Table of contents
- Top 3 Methods to Count Colored Cells In Excel
- #1 – Excel Count Colored Cells By Using Auto Filter Option
- #2 – Excel Count Colored Cells by using VBA Code
- #3 – Excel Count Colored Cells by Using FIND Method
- Things to Remember
- Recommended Articles
Now, let us discuss each of them in detail –
#1 – Excel Count Colored Cells By Using Auto Filter Option
For this example, let us look at the below data.
As we can see, each city is marked with different colors. So, we need to count the number of cities based on cell color.
As we can see, all the colors in the data. Now, we must choose the color that we want to filter.
We must follow the below steps to count cells by color.
- We must first apply the filter to the data.
- At the bottom of the data, we need to apply the SUBTOTAL function in Excel to count cells.
- The SUBTOTAL function contains many formulas. It is helpful if we want to count, sum, and average only visible cell data. Under the heading PIN, we must click on the drop-down list filter and select Choose by Color.
- As we can see, all the colors in the data. Now, we must choose the color that we want to filter.
Wow!!! As we can see in cell D21, our SUBTOTAL function is given the count of filtered cells as 6 instead of the previous result of 18.
Similarly, now we must choose other colors to get the count of the same.
So, blue-colored cells count to five now.
#2 – Excel Count Colored Cells by using VBA Code
VBA’s street smart techniques help us reduce time consumption at our workplace for some complicated issues.
We can reduce time, but we can also create our functions to fit our needs. For example, we can create a function to count cells based on color in one such function. Below is the VBA codeVBA code refers to a set of instructions written by the user in the Visual Basic Applications programming language on a Visual Basic Editor (VBE) to perform a specific task.read more to create a function to count cells based on color.
Code:
Function Color_Cell_Count(ColorCell As Range, DataRange As Range) Dim Data_Range As Range Dim Cell_Color As Long Cell_Color = ColorCell.Interior.ColorIndex For Each Data_Range In DataRange If Data_Range.Interior.ColorIndex = Cell_Color Then Color_Cell_Count = Color_Cell_Count + 1 End If Next Data_Range End Function
Then, copy and paste the above code to your module.
This code is not a SUB Procedure to run. Rather, it is a “User Defined FunctionUser Defined Function in VBA is a group of customized commands created to give out a certain result. It is a flexibility given to a user to design functions similar to those already provided in Excel.read more” (UDF).
The first line of the code “Color_Cell_Count” is the function name. Now, we must create three cells and color them as below.
Now, we must open the function “Color_Cell_Count” in the G2 cell.
Even though we do not see the syntax of this function, the first argument is what color we need to count, so we must select cell F2.
The second argument is to select the range of cells as D2:D19.
Now, close the bracket and press the “Enter” key. As a result, it will provide the count of cells with the selected cell color.
Like this, with the help of UDF in VBA, we can count cells based on cell color.
#3 – Excel Count Colored Cells by Using FIND Method
We can also count cells based on the FIND method as well.
- Step 1: First, we must select the range of cells where we need to count cells.
- Step 2: Now, we need to press Ctrl + F to open the FIND dialog box.
- Step 3: Now, click on “Options>>.”
- Step 4: Consequently, it will expand the “Find” dialog box. Now, we must click on the “Format” option.
- Step 5: Now, it will open up the “Find Format” dialog box. We need to click on the “Choose Format From Cell” option.
- Step 6: Now, move the mouse pointer to see the pointer to select the format cell in excelFormatting cells is an important technique to master because it makes any data presentable, crisp, and in the user’s preferred format. The formatting of the cell depends upon the nature of the data present.read more that we are looking to count.
- Step 7: We will select the cell formatted as the desired cell count. We have chosen the F2 cell as the desired cell format, and now we can see the preview.
- Step 8: Now, click on the “Find All” option to get the count of the selected cell format count of cells.
So, a total of 6 cells were found with selected formatting colors.
Things to Remember
- The provided VBA code is not a Subprocedure in VBASUB in VBA is a procedure which contains all the code which automatically gives the statement of end sub and the middle portion is used for coding. Sub statement can be both public and private and the name of the subprocedure is mandatory in VBA.read more; it is a UDF.
- The SUBTOTAL contains many formulas used to get the result only for visible cells when the filter is applied.
- We do not have any built-in function in Excel to count cells based on the color of the cell.
Recommended Articles
This article has been a guide to Count Colored Cells in Excel. We learned to count colored cells using the auto filter option, VBA code, FIND method, and downloadable Excel template. You may learn more about Excel from the following articles: –
- Shade Alternate Rows in Excel
- Countif not Blank in Excel
- Count Rows in Excel
- Word Count in Excel
Count Colored Cells in Excel (Table of Contents)
- Introduction to Count Colored Cells in Excel
- How to Count Colored Cells in Excel?
Introduction to Count Colored Cells in Excel
We all are well aware of the fact that COUNT and COUNTIF functions are the ones that can be used to count the cells with numbers and cells that follow any specific criteria. Isn’t it being fancy to have a function which counts the colored cells? I mean something that counts different cells based on their color? Unfortunately, there is no such function directly available in Excel. However, we can use a combination of different functions to achieve the same. Thanks to the versatility of Microsoft Excel. Therefore, here are the methods using which we can count the number of colored cells in Excel.
- Using SUBTOTAL formula and color filters.
- Using COUNTIF and GET.CELL to count colored cells.
How to Count Colored Cells in Excel?
Let’s understand how to Count Colored Cells in Excel with a few examples.
You can download this Count Colored Cells Excel Template here – Count Colored Cells Excel Template
Example #1 – Using SUBTOTAL Formula and Color Filters
Suppose we have data of 1000 colored rows as shown below, and all we wanted is to find out the count of each color within the entire data. See the partial layout of the data given below:
Step 1: Go to cell F1002 to use the SUBTOTAL formula to capture the subtotal.
Step 2: Start typing the formula for SUBTOTAL inside the cell F1002. This formula has several function options within it. You can see the list as soon as the formula is active.
Step 3: Out of the list, you need to select function number 102 associated with the COUNT function. Use the mouse to drag towards function 102 and double click it to select.
Step 4: Use cells F1 to F10001 as a reference array within which the function COUNT will be used to count the cells. Fix the cell reference with the dollar sign, as shown in the screenshot below.
Step 5: Close the parentheses to complete the formula and press Enter key to see the output. You can see 1000 as a number in cell F1002.
Step 6: Now apply a filter to the entire data and use the Filter by Cell Color option to filter the data with cell colors. As soon as you filter a specific color, the value of the cell F1002 will change to the number of cells that are with that specific color. See the screenshots below.
You can see there are 419 cells which have the same color as filtered, i.e. light orange. You similarly can check the count of other colors as well.
This works fine because of the use of the COUNT function (102) within the SUBTOTAL as an argument. The COUNT function counts all the non-empty cells within the given range, and subtotal gives the total of the counts. If we apply a filter with any specific criteria, due to the nature of SUBTOTAL, we only get the total count associated with those criteria (in this case, color).
Example #2 – Using COUNTIF and GET.CELL
We can also use the COUNTIF and a GET.CELL function in combination to get the count of colored cells. GET.CELL is an old macro function which does not work with most of the functions. However, it has compatibility with named ranges. This function can extract the extra piece of information which a normal CELL function could not extract. We use this function to get information about the color of a cell. Follow the steps to know how we can count the cells with color using these two functions.
Step 1: Create a new named range ColorCode using the named ranges option within the Formulas tab.
Go to Formulas tab > inside Defined Names section, click on Define Name to be able to create a named range.
Step 2: A pop-up box with “New Name” will appear. Mention the name as ColorCode and within Refers to: section, use the formula as =GET.CELL(38,’Example #2′!$A2). Press OK after the amendments. Your named range will be completed and ready to use.
38 here is an operation number associated with GET.CELL. It is associated with an operation that captures the unique color numbers in Excel.
Rename column G as ColorCode, where we will capture the numeric codes associated with each color. These codes are unique for each color.
Step 3: In cell G2, start typing =ColorCode. This is the named range which we have just created in the previous step. Press Enter key to get a unique numeric code associated with the color of cell A2 (remember, while defining the named range, we used A2 as a reference from a sheet).
40 is the code associated with pink color in Excel.
Step 4: Drag and paste the formula across cell G3:G1001, and you’ll see different numbers, which are nothing but the color codes associated with each color. See the partial screenshot below.
Step 5: Now, we need to calculate what is the count of each color in the sheet. For this, we will use the COUNTIF function. Start typing =COUNTIF in cell B1003 of the given excel sheet.
Please note that we have colored the cell 1003:1006 with the colors we have used respectively in our example.
Step 6: Use column G as a range argument to this function and ColorCode named range as a criterion. Complete the formula by closing the parentheses and see the output in cell B1003.
After using the formula, the output is shown below.
Drag and paste this formula across the remaining cells and see the count of remaining colors.
In this article, we have seen the different methods which can be used to find out the count of colored cells. Let’s wrap the things with some points to be remembered.
Things to Remember About Count Colored Cells In Excel
- We don’t have any built-in function which can count the colored cells in the excel sheet. However, we can use a different combination of formulae to capture the same.
- The CELL function, which we use in the second example, gives more information related to a cell that the general CELL function can’t provide.
- Numeric code 102 we use under the SUBTOTAL function is a numeric code of COUNT function under it.
Recommended Articles
This is a guide to Count Colored Cells in Excel. Here we discuss How to use Count Colored Cells in Excel along with practical examples and a downloadable excel template. You can also go through our other suggested articles –
- Count Cells with Text in Excel
- Count Characters in Excel
- COUNTIFS in Excel
- How to Add Cells in Excel
Sometimes you may use different colored cells in Google Sheets to represent categories. This makes your data easy to read. In such cases, it can be useful to count the cells based on the cell’s color. For instance, we have a list of student names and they’re colored. If the cell is red in color, the student is absent. If the cell is green in color, the student is present.
If you want to know the number of students present, it’d be useful if you could simply count the green-colored cells, right? So we will show you how to do that in easy steps, plus there are four different ways in which you can count the colored cells.
Let us begin!
4 Methods of Counting Colored Cells in MS Excel:
- Using SUBTOTAL and filters
- Using VBA code
- Using the FIND function
- Using COUNTIF and GET.CELL
Method #1: Using SUBTOTAL and Filters
This method uses the SUBTOTAL formula to calculate the total number of cells. You can then use the filter to only see cells of a particular color.
Step #1: Pick a cell to display the count
Select a cell where you want to see the results of counting all the colored cells.
In our example, we’ve chosen C1 to display the count. You can see the selected cell in the Name Box on the top left corner just below the toolbar.
Step #2: Apply the SUBTOTAL formula
Capture the subtotal formula in cell C1.
The formula is [=SUBTOTAL(function_name, ref1)].
function_name is represented by numeric values 1 to 11 and 101 to 111.
For this purpose, you can use either 102 or 103. You can use 102 if you’re counting cells with numeric data. You can use 103 if you’re counting cells with text data.
Click on cell C1.
Go to the Formulas tab in the top menu bar and select Math & Trig, scroll down to SUBTOTAL and click on it.
Now, fill the Function_num and Ref1 fields and click on OK.
For our example, we will use function_num 103 and the Fields are A2 to A20 (A2:A20).
Here, the formula would be =SUBTOTAL(103, A2:A20).
Alternatively, you can directly type the formula into the cell.
Step #3: Press Enter
If you have used the inserted the formula using the menu bar, click on the OK button.
If you have typed in the formula, press Enter to see the formula in action.
You would now see the total count of all the cells.
Note: The SUBTOTAL formula with function numbers between 101 and 111 calculates visible cells. If you use another formula, it will count the number of cells regardless if they are visible or not.
Step #4: Apply filter by cell color
Now let’s apply a filter for the green-colored cells to get the count of students who are present.
Under the main menu Home tab, click Sort & Filter, and then from the dropdown, select Filter.
Alternatively, you can click on the alphabetic header row and press Ctrl+Shift+L.
Once you have applied a filter to your table, you will see a small block with a downward pointing arrow next to each header cell. This is the Filter Icon.
Click on the Filter Icon.
Select Filter by Color.
Click on the color you want to filter and have visible.
In our example, we picked the green color.
Click on the OK button.
You will now only see the green-colored cells in Excel.
In addition, the SUBTOTAL formula in C1 now only gives you the total number of green cells.
Method #2: Using VBA code
In this method, you create a custom or user-defined function. With the created function, you can count the colored cells.
Step #1: Select Visual Basic
Go to the Developer tab in the main menu bar and select Visual Basic.
Step #2: Create a module
You can now see a visual basic application dialog box.
Click on the minus sign at the top left.
Click on Insert and from the submenu that appears, select Module.
Step #3: Copy-paste the VBA code
Once you click on Module, a dialog box will be launched. Copy the following code.
Function Color_Cell_Count(ColorCell As Range, DataRange As Range)
Dim Data_Range As Range
Dim Cell_Color As Long
Cell_Color = ColorCell.Interior.ColorIndex
For Each Data_Range In DataRange
If Data_Range.Interior.ColorIndex = Cell_Color Then
Color_Cell_Count = Color_Cell_Count + 1
End If
Next Data_Range
End Function
Source
Now paste the code into the dialog box.
Close the dialog box by clicking on the red X icon at the top-right corner.
Step #4: Apply the code in your spreadsheet
Color two cells, the same as the source formatting. In our example, that would be red and green.
C2 is red in color and D2 will represent the number of red-colored cells.
C3 is green in color and D3 will represent the number of green-colored cells.
Place your cursor in D2, and enter the formula =Color_Cell_Count(C2, A2:A20).
C2 represents a cell with the color you want to count, while A2:A20 represents the range of cells to be counted.
Step #5: Press Enter
Press Enter to see the count.
Similarly, copy the same formula onto the next cell to find the number of green-colored cells.
Method #3: Using the FIND function
In this method, you use the FIND function to get the number of cells based on formatting. Here, the formatting is colored cells.
Step #1: Select the range of cells
Select the range of cells by clicking and dragging your cursor across them.
The Name Box will show you the selected range.
Step #2: Open ‘Find and Replace’ dialog box
Press Ctrl and the letter F on your keyboard simultaneously to open the Find and Replace dialog box.
Select Options in the dialog box.
Step #3: Go to ‘Format’
Click on the Format button.
This will open the Find Format dialog box.
Step #4: Select ‘Choose Format From Cell’
In the Find Format dialog box, at the bottom you will see the ‘Choose Format From Cell…’ option.
Click on it.
The window will now minimize, and your mouse cursor will change to look like a pen icon.
Tap on any cell with the color that you want to count.
In our example, we pick a green-colored cell.
Step #5: Select ‘Find All’
The Find and Replace window will now open again, where you will see the chosen color in Preview.
Click on the Find All button to find all such cells with the same format.
You will see a list of all the green-colored cells along with their count at the bottom of the window.
Method #4: Using COUNTIF and GET.CELL
In this method, we first get the unique color code and then count the cells using the color code.
Step #1: Go to ‘Formulas’ and click on ‘Define Name’
In the Formulas tab in the main menu bar, click on Define Name.
Select Define Name again from the dropdown that appears.
Step #2: Enter details for Define Name
A new window will appear, with fields to be completed.
In the Name: field, enter any name you like. Here, we’ve entered ColorCode.
Type the formula =GET.CELL(38,Sheet1!$A2) in the ‘Refers to:’ field.
In our example, 38 represents the unique color number in Excel.
The next parameter is the cell reference, i.e. cell A2 in Sheet 1.
Click on the OK button once you’re done.
Step #3: Get the color code
Now go to your Excel sheet.
In the next column, B, in our example, enter =ColorCode.
In your case you would use the name you assigned.
Now, drag the fill handle across all the rows with data. The fill handle is a the little square at the bottom right of your selected cell.
This would ensure you get the code for all your colored cells.
Step #4: Apply the COUNTIF formula
In column D, fill two cells with the same colors as in Column A.
Next you will count the different colors matching the colors in column D.
Place your cursor in column E, next to the colored cell in column D, and enter the formula.
The formula is =COUNTIF(range, criteria).
We use the color code to calculate.
In our example, the range is from B2 to B20.
Criteria is the name we created, ColorCode.
Therefore our formula would become =COUNTIF(B2:B20, ColorCode).
Step #5: Press Enter
Once you’re done entering the formula, press Enter.
This will give you the count of cells with the color in the cell left to it.
Drag the fill handle down to the next cell, to get the count for the other color as well.
The Bottom Line
No more struggling to count the number of colored cells. These four methods will help you get the count of the colored cells in no time!
Содержание
- Count Colored Cells in Excel
- Top 3 Methods to Count Colored Cells In Excel
- #1 – Excel Count Colored Cells By Using Auto Filter Option
- #2 – Excel Count Colored Cells by using VBA Code
- #3 – Excel Count Colored Cells by Using FIND Method
- Things to Remember
- Recommended Articles
- How to Count Colored or Highlighted Cells in Excel
- Using Subtotal and Filter functions
- Using COUNTIF and GET.CELL functions
- Using VBA
- How to sum and count cells by color in Excel
- How to count cells by color in Excel
- Count cells by fill color
- Count cells by font color
- How to sum by color in Excel
- Sum values by cell color
- Sum values by font color
- Count and sum by color across entire workbook
- How to count colored cells in entire workbook
- How to sum colored cells in whole workbook
- Count and sum conditionally formatted cells
- How to count and sum conditionally formatted cells using VBA macro
- How to get cell color in Excel
- Get fill color of a cell
- Get font color of a cell
- Get hexadecimal color code of a cell
- How to insert VBA code in your workbook
- How to get custom functions to update
- Fastest way to calculate colored cells in Excel
- Sum and count cells by one color
- Count and sum all colored cells at once
Count Colored Cells in Excel
Top 3 Methods to Count Colored Cells In Excel
There is no built-in function to count colored cells in Excel, but below mentioned are three different methods to do this task.
- Count colored cells by using the Auto Filter option
- Count colored cells by using the VBA code
- Count colored cells by using the FIND method
Table of contents
Now, let us discuss each of them in detail –
#1 – Excel Count Colored Cells By Using Auto Filter Option
For this example, let us look at the below data.
As we can see, each city is marked with different colors. So, we need to count the number of cities based on cell color.
As we can see, all the colors in the data. Now, we must choose the color that we want to filter.
We must follow the below steps to count cells by color.
At the bottom of the data, we need to apply the SUBTOTAL function in Excel to count cells.
The SUBTOTAL function contains many formulas. It is helpful if we want to count, sum, and average only visible cell data. Under the heading PIN, we must click on the drop-down list filter and select Choose by Color.
As we can see, all the colors in the data. Now, we must choose the color that we want to filter.
Wow. As we can see in cell D21, our SUBTOTAL function is given the count of filtered cells as 6 instead of the previous result of 18.
Similarly, now we must choose other colors to get the count of the same.
So, blue-colored cells count to five now.
#2 – Excel Count Colored Cells by using VBA Code
VBA’s street smart techniques help us reduce time consumption at our workplace for some complicated issues.
We can reduce time, but we can also create our functions to fit our needs. For example, we can create a function to count cells based on color in one such function. Below is the VBA code VBA Code VBA code refers to a set of instructions written by the user in the Visual Basic Applications programming language on a Visual Basic Editor (VBE) to perform a specific task. read more to create a function to count cells based on color.
Code:
Then, copy and paste the above code to your module.
The first line of the code “Color_Cell_Count” is the function name. Now, we must create three cells and color them as below.
Now, we must open the function “Color_Cell_Count” in the G2 cell.
Even though we do not see the syntax of this function, the first argument is what color we need to count, so we must select cell F2.
The second argument is to select the range of cells as D2:D19.
Now, close the bracket and press the “Enter” key. As a result, it will provide the count of cells with the selected cell color.
Like this, with the help of UDF in VBA, we can count cells based on cell color.
#3 – Excel Count Colored Cells by Using FIND Method
We can also count cells based on the FIND method as well.
- Step 1: First, we must select the range of cells where we need to count cells.
- Step 2: Now, we need to press Ctrl + F to open the FIND dialog box.
- Step 3: Now, click on “Options>>.”
- Step 4: Consequently, it will expand the “Find” dialog box. Now, we must click on the “Format” option.
- Step 5: Now, it will open up the “Find Format” dialog box. We need to click on the “Choose Format From Cell” option.
- Step 6: Now, move the mouse pointer to see the pointer to select the format cell in excelFormat Cell In ExcelFormatting cells is an important technique to master because it makes any data presentable, crisp, and in the user’s preferred format. The formatting of the cell depends upon the nature of the data present.read more that we are looking to count.
- Step 7: We will select the cell formatted as the desired cell count. We have chosen the F2 cell as the desired cell format, and now we can see the preview.
- Step 8: Now, click on the “Find All” option to get the count of the selected cell format count of cells.
So, a total of 6 cells were found with selected formatting colors.
Things to Remember
- The provided VBA code is not a Subprocedure in VBASubprocedure In VBASUB in VBA is a procedure which contains all the code which automatically gives the statement of end sub and the middle portion is used for coding. Sub statement can be both public and private and the name of the subprocedure is mandatory in VBA.read more ; it is a UDF.
- The SUBTOTAL contains many formulas used to get the result only for visible cells when the filter is applied.
- We do not have any built-in function in Excel to count cells based on the color of the cell.
Recommended Articles
This article has been a guide to Count Colored Cells in Excel. We learned to count colored cells using the auto filter option, VBA code, FIND method, and downloadable Excel template. You may learn more about Excel from the following articles: –
Источник
How to Count Colored or Highlighted Cells in Excel
The COUNT function in Excel counts cells containing numbers in Excel. You cannot count colored or highlighted cells with the COUNT function. But you can follow a few workarounds to count colored cells in Excel. In this tutorial, you will learn how to count colored cells in Excel.
In excel, you can count highlighted cells using the following workarounds:
- Applying SUBTOTAL and filtering the data
- Using the COUNT and GET.CELL function
- Using VBA
Using Subtotal and Filter functions
You can count highlighted cells in Excel by subtotaling the visible cells and applying a filter based on colors.
In this example, you have the sales record for eight salespersons for the month of July. The rows containing salespersons having sales less than $7000 is highlighted in red, the other cells with the salespersons having a bonus is highlighted in green. To calculate the number of salespersons highlighted in red:
- Select the cell C10.
- Assign the formula = SUBTOTAL(102, C2:C9) . The first argument 102 counts the visible cells in the specified range.
- Select cells A1:C9 by clicking on cell A1 and dragging it till C9 with your mouse.
- Go to Data > Sort & Filter > Filter.
- Click on the filter drop downs in C1.
- Go to Filter by Color and select the color Red to find out the salespersons highlighted in red.
This will output change the subtotal value to x, which is the number of salespersons having sales less than $7000.
Using COUNTIF and GET.CELL functions
You can use GET.CELL with named ranges to count colored cells in Excel. GET.CELL is an old Macro4 function and does not work with regular functions. However, it still works with named ranges. To count colored cells with GET.CELL, you need to extract the color codes with GET.CELL and count them to find out the number of cells highlighted in the same color. To count cells using GET.CELL and COUNTIF:
- In the dialogue box that pops up, set name as ColorCount, scope as workbook and Refers to as =GET.CELL(38, Sheet1!C2).
- Assign the formula =ColorCount to cell D2 and drag it till D9 with your mouse.
- This would return a number based on the background color. If there is no background color, Excel would return 0. Otherwise all cells with the same background color return the same number.
- From column D, look at the code for the color red. In this case it is 22.
- Select cell D10 and assign the formula = COUNTIF($D:$D, 22).
This will count the cells colored in red to find the number of salespersons with sales less than $7000 and return the number 3.
Using VBA
You can also create a custom function with VBA to count highlighted cells in Excel. To do that you need to create a custom function using VBA that works like a COUNTIF function and returns the number of cells for the same color.
You will follow the syntax: =CountFunction(CountColor, CountRange) and use it like other regular functions.Here CountColor is the color for which you want to count the cells. CountRange is the range in which you want to count the cells with the specified background color.
To count the cells highlighted in red, follow the steps below:
- Hold down the ALT + F11 keys, and it opens the Microsoft Visual Basic for Applications window.
- Click Insert >Module .
- Paste the following code in the Module Window.
- In this code, you are defining a function with two arguments rColor and rRange . You are going to save the value of the background color of A2 in lCol. Then you are going to run a FOR loop where, if the cell’s background color matches the color in lCol, you increment vResult . This function returns the value of vResult, which is the count of the cells having the same background color.
- Then save the code, and apply the following formula to cell C10:
- Here A2 is the cell with the background color red you want to count. C2:C10 is the cell range that you want to count.
This will return the number of salespersons with sales less than $7000 in the month of July which is 3.
Источник
How to sum and count cells by color in Excel
by Svetlana Cheusheva, updated on January 20, 2023
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 🙂
How to count cells by color in Excel
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.
Custom functions to count by color in Excel
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.
Count cells by fill color
To count cells with a particular background color, this is the function to use:
- Data_range is a range in which to count cells.
- Cell_color is a reference to the cell with the target fill color.
To count cells of a specific color in a given range, carry out these steps:
- Insert the code of the CountCellsByColor function in your workbook.
- In a cell where you want the result to appear, start typing the formula: =CountCellsByColor(
- For the first argument, enter the range in which you want to count colored cells.
- For the second argument, supply the cell with the target color.
- Press the Enter key. Done!
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:
Count cells by font color
In case your cell values have different font colors, you can count them using this function:
- Data_range is a range in which to count cells.
- Font_color is a reference to the cell with the sample font color.
For example, to get the number of cells in B3:F24 whose values have the same font color as H3, the formula is:
=CountCellsByFontColor(B3:F24, H3)
Tip. If you’d like to name the functions differently, feel free to change the names directly in the code.
How to sum by color in Excel
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.
Custom functions to sum by color in Excel
Sum values by cell color
To sum by fill color in Excel, this is function to use:
- Data_range is a range in which to sum values.
- Cell_color is a reference to the cell with the fill color of interest.
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:
=SumCellsByColor(B3:F24, H3)
Sum values by font color
To sum numeric values with a specific font color, use this function:
- Data_range is a range in which to sum cells.
- Font_color is a reference to the cell with the target font color.
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:
=SumCellsByFontColor(B3:F24, H3)
Count and sum by color across entire workbook
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:
Custom functions to count and sum by color across workbook
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.
How to count colored cells in entire workbook
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.
How to sum colored cells in whole workbook
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:
Count and sum conditionally formatted cells
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.
VBA macro to count and sum conditionally formatted cells.
How to count and sum conditionally formatted cells using VBA macro
With the macro’s code inserted in your Excel, this is what you need to do:
- Select one or more ranges where you want to count and sum colored cells. Make sure the selected range(s) contains numerical data.
- Press Alt + F8 , select the SumCountByConditionalFormat macro in the list, and click Run.
- A small dialog box will pop asking you to select a cell with the sample color. Do this and click OK.
For this example, we used the inbuilt Highlight Cell Rules and got the following results:
- Count (12) the number of cells in range B2:E22 with the same color as G3.
- Sum (1512) is the sum of values in cells formatted with Light Red Fill.
- Color is a hexadecimal color code of the sample cell.
Tip. The sample workbook with the SumCountByConditionalFormat macro is available for download at the end of this post.
How to get cell color in Excel
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.
Custom functions to get the cell color
Note. The functions only work for colors applied manually, and not with conditional formatting.
Get fill color of a cell
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:
Get font color of a cell
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:
Get hexadecimal color code of a cell
To convert a decimal color index returned by our custom functions into a hexadecimal color code, make use of Excel’s native DEC2HEX function.
=»#»&DEC2HEX(GetFontColor(E2))
How to insert VBA code in your workbook
To add the function’s or macro’s code to your Excel, move on with these 4 steps:
- In your workbook, press Alt + F11 to open Visual Basic Editor.
- In the left pane, right-click on the workbook name, and then choose Insert >Module from the context menu.
- In the Code window, insert the code of the desired function(s):
- Count by color: CountCellsByColor and CountCellsByFontColor functions
- Sum by color: SumCellsByColor and SumCellsByFontColor functions
- Count and sum colored cells in whole workbook: WbkCountByColor and WbkSumByColor functions
- Count and sum conditionally formatted cells: SumCountByConditionalFormat function
- Get color code: GetCellColor and GetFontColor functions
- Save your file as Macro-Enabled Workbook (.xlsm).
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.
How to get custom functions to update
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.
Fastest way to calculate colored cells 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:
- Count and sum cells by color in all versions of Excel 2016 — Excel 365.
- Find average, maximum and minimum values in the colored cells.
- Handle cells colored manually and with conditional formatting.
- Paste the results anywhere in a worksheet as values or formulas.
Sum and count cells by one color
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:
Count and sum all colored cells 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.
Источник
The COUNT function in Excel counts cells containing numbers in Excel. You cannot count colored or highlighted cells with the COUNT function. But you can follow a few workarounds to count colored cells in Excel. In this tutorial, you will learn how to count colored cells in Excel.
In excel, you can count highlighted cells using the following workarounds:
- Applying SUBTOTAL and filtering the data
- Using the COUNT and GET.CELL function
- Using VBA
Using Subtotal and Filter functions
You can count highlighted cells in Excel by subtotaling the visible cells and applying a filter based on colors.
In this example, you have the sales record for eight salespersons for the month of July. The rows containing salespersons having sales less than $7000 is highlighted in red, the other cells with the salespersons having a bonus is highlighted in green. To calculate the number of salespersons highlighted in red:
- Select the cell C10.
- Assign the formula
=
SUBTOTAL(102, C2:C9)
. The first argument 102 counts the visible cells in the specified range.
- Select cells A1:C9 by clicking on cell A1 and dragging it till C9 with your mouse.
- Go to Data > Sort & Filter > Filter.
- Click on the filter drop downs in C1.
- Go to Filter by Color and select the color Red to find out the salespersons highlighted in red.
This will output change the subtotal value to x, which is the number of salespersons having sales less than $7000.
Using COUNTIF and GET.CELL functions
You can use GET.CELL with named ranges to count colored cells in Excel. GET.CELL is an old Macro4 function and does not work with regular functions. However, it still works with named ranges. To count colored cells with GET.CELL, you need to extract the color codes with GET.CELL and count them to find out the number of cells highlighted in the same color. To count cells using GET.CELL and COUNTIF:
- Go to Formulas > Define Name.
- In the dialogue box that pops up, set name as ColorCount, scope as workbook and Refers to as
=GET.CELL(38, Sheet1!C2).
- Assign the formula =ColorCount to cell D2 and drag it till D9 with your mouse.
- This would return a number based on the background color. If there is no background color, Excel would return 0. Otherwise all cells with the same background color return the same number.
- From column D, look at the code for the color red. In this case it is 22.
- Select cell D10 and assign the formula
= COUNTIF($D:$D, 22).
This will count the cells colored in red to find the number of salespersons with sales less than $7000 and return the number 3.
Using VBA
You can also create a custom function with VBA to count highlighted cells in Excel. To do that you need to create a custom function using VBA that works like a COUNTIF function and returns the number of cells for the same color.
You will follow the syntax: =CountFunction(CountColor, CountRange) and use it like other regular functions.Here CountColor is the color for which you want to count the cells. CountRange is the range in which you want to count the cells with the specified background color.
To count the cells highlighted in red, follow the steps below:
- Hold down the ALT + F11 keys, and it opens the Microsoft Visual Basic for Applications window.
- Click Insert > Module.
- Paste the following code in the Module Window.
- In this code, you are defining a function with two arguments rColor and rRange. You are going to save the value of the background color of A2 in lCol. Then you are going to run a FOR loop where, if the cell’s background color matches the color in lCol, you increment vResult. This function returns the value of vResult, which is the count of the cells having the same background color.
- Then save the code, and apply the following formula to cell C10:
=ColorCount(A2, C2:C10).
- Here A2 is the cell with the background color red you want to count. C2:C10 is the cell range that you want to count.
This will return the number of salespersons with sales less than $7000 in the month of July which is 3.