Even though Microsoft Excel has a variety of functions for different purposes, none can count or sum by color of a cell. Aside from third-party tools, there is only one possible solution - create your own functions. Continue reading
by Svetlana Cheusheva, updated on
Even though Microsoft Excel has a variety of functions for different purposes, none can count or sum by color of a cell. Aside from third-party tools, there is only one possible solution - create your own functions. Continue reading
Comments page 18. Total comments: 838
Hello and thank you for this great formula!
I have 1 thing I cant seem to figure out.
I am using the formula from answer in question 184, but I can't seem to use it to count cells with any text (*)
I have tried this;
=SUM(('1sthalf'!X6:X23="*")*(GetCellColor('1sthalf'!X6:X23)=GetCellColor(A1)))
The above returns 0 as the value, there is 1 cell with text, so answer should be 1.
Meaning I want it to count all white fields (A1 is white) where there is text in cells X6:X23. It works once I just write "tr" which is the only cell with text, but sometimes it is many different combinations of text in the cells.
I hope I dont have to copy this formula for every different text string I have in my range? I have approximately 100 different text strings, so I doubt excel has room for such a big formula.
Thanks!
Hello Nicolai,
Please try to use the following formula instead:
=SUM(('1sthalf'!X6:X23<>"")*(GetCellColor('1sthalf'!X6:X23)=GetCellColor(A1)))
The asterisk character works only for the search.
Hi Rohit,
Covert the data range to a Table.[Ctrl+T]function.
Highlight the column[s] you want to sum/average/mean etc and set the Conditional formatting.
In the column header of the table you can use the filter to total by selecting the colour condition you have set.
Can you please write out the code? I'm new to VBA.
Thanks in advance
Hi Ramki,
Thank you very much for your reply!
Rohit, this is one of the ways to sum or count the number of cells with a certain color set by conditional formatting rules.
The second way would be using the VBA script that you can find in the section How to count by color and sum cells colored using conditional formatting.
When i am using the GetCellColor function by passing the cell number, no matter whatever the background color of the cell is, it is always returning the value 16777215.
Could you kindly help?
I was able to figure out the problem. I have conditional formatting rules set up on the cells, which gives an impression that the cell has some background color, however in reality there is no color in the background. This function is picking the default cell color in the background instead of the one from conditional formatting. If i use this function on any other cell without having conditional formatting, it returns the correct value.
Is there way to use this function to pick up conditional formatting color?
Hello Rohit,
Unfortunately there is no sure way to get the conditional formatting color with the help of user-defined functions, please see the section "How to count by color and sum cells colored using conditional formatting" for more details.
Hi,
I can't seem to get the countcellsbycolor function to work, do to the comma(,) in the formula. did i miss something?
Hi Alex,
Most likely you have a custom locale in Excel that uses a different delimiter for separating values in the formula. Please go to Control Panel -> Region and Language-> Additional settings and replace the delimiter to get the formula to work. If you still encounter any issues, please send your spreadsheet to support@ablebits.com, we’ll take a look at it.
when i use this code its gives me a error
Compile error:
Syntax error
(Module1 48:0)
Hello Singh,
You can download and use this sample workbook with the CountCellsByColor and SumCellsByColor functions ready for use and try them on your data.
Great job Svetlana on this "How to count and sum cells by color in Excel 2010 and 2013"
Hello mam,
i want to count quantity of particular items,its created by different codes like ABC,ADB,ACD....etc. Now i want to count a particular item means ABC=qty ?.
I used =countif(D4:D52,"ABC") but it counts only no of codes in sheet,i want to count qty of ABC its mentioned in other coloumn and ABC code is repeating below raws. i want to calculate the qty of ABC,ADB,ACD...etc
How it possible ???
Hello Robin,
You can use the SUMIF function like:
=SUMIF(D4:D52,"ABC", B4:B52)
Where column D contains your ABC, ADB, ACD, etc, code, and column B is the quantity to sum.
Change Room CFL2 CFL 2Pin 11 Watts X 2 nos 1
Liquid manufacturing PL3 PL Type3 36Watts X 3 nos 6
Liquid Filling PL3 PL Type3 36Watts X 3 nos 2
Liquid Material Entry CFL2 CFL 2Pin 11 Watts X 2 nos 1
Liquid Washing PL2 PL Type2 36Watts X 2 nos 1
Liquid Bottle Wash TR2 Tube Rod 2ft 14 watts X 2 nos. 1
Liquid Packing TR2 Tube Rod 2ft 14 watts X 2 nos. 8
Sugar Storage PL2 PL Type2 36Watts X 2 nos 5
Carton Coding PL2 PL Type2 36Watts X 2 nos 1
Production Entrance CFL2 CFL 2Pin 11 Watts X 2 nos 1
Hey Martin, would be great if you could help me for above query....
your functions are quite good to use, Is there anything where I can count basis on a given condition in cell i.e 2015 (with green color), note- there are other cell as well with 2015 red color, but I need the count for 2015 with green cell. please help
Regards
Mahesh
Hi Mahesh,
Sorry for the delay,
Please copy the VBA code to your workbook and then enter the following array formula (remember to press Ctrl + Shift + Enter to complete it):
=SUM((A1:B13="2015")*(GetCellColor(A1:B13)=GetCellColor(D1)))
Where A1:B13 is the range where you want to count the occurrence of the text "2015", and
D1 is the helper cell with the color sample, e.g. green.
You can also type =GetCellColor(D1) in any cell, copy the result and paste it to your formula instead of calling GetCellColor(D1). In this case you don't need the helper cell.
HI Alex,
I am trying to count the number of time a cell is red and has the number 33 in it. When I try to create the array you posted on Feb 11, 2016 the value I receive in the cell is 0, when the answer should be 2. I am using =SUM((C2:C132=G13)*(getcellcolor(C1:C132)=getcellcolor(G4)))
Thanks for the help!
Hello Phyllis!
In your formula =SUM((C2:C132=G13)*(getcellcolor(C1:C132)=getcellcolor(G4)))
instead of C1 use C2
=SUM((C2:C132=G13) * (getcellcolor(C2:C132)=getcellcolor(G4)))
I hope this will help, otherwise please do not hesitate to contact me anytime.
Sounds about like what I need. Have you gotten any response yet?
Oh, never mind; found what I need here: https://www.ablebits.com/office-addins-blog/array-formulas-functions-excel/#howto-enter-array-formula
What a great site this is! My toolbox just doubled in size!
Thank you very much for your kind words, Lisa. Though we can't guarantee a quick reply due to a big volume of comments we get, feel free to ask any questions you have if anything remains unclear.
Hi, I have tried to use your formula with a SUMIF formula. I have two columns; column A which is a text entry cell (project name) and column B which is a currency cell. I want a formula where I can add the sum of the cells in column B but only if the text colour in Column A is green. I tried using the formula:
=SUMIF(A5:A12,"SumCellsByColor(A5:A12,A19)", B5:B12)
where A19 says "Total Green Projects" in a green font. Whilst you might wonder why I don't just change the text colour in the B column and use your regular formula, in reality the spread sheet is far more complex and column B is actually many columns away from column A (and usually in a variety of different colours). If you could help in any way that would be really appreciated!
Hello, Martin,
Thank you for contacting and for sending us your sample workbook. We have just replied to you by email, please check your inbox.
Thank you for the spread sheet however it doesn't work when I change the colour of the text of the cells in Column A; the change in value is not reflected in the formula.
I also see that if I insert a new row, the formula shows "VALUE!".
I want to be able to change the colour of any cells in column A (to green, or any other colour) and for the corresponding increase/decrease in the total sum of column B, which is displayed in the formula cell. As new projects arrive, I want to be able to insert new rows and have this included in the formula.
As this is a very fluid spread sheet which is always changing, the formula doesn't seem to work with any changes. Is this something that can be fixed? Is this possible?
Hi Martin,
We apologize for the delay in our reply, we get so many comments we’re having a hard time answering them all. Practically anything is possible :) Could you please send the spreadsheet with the formula you inserted to support@ablebits.com? It is very likely that you need to fix the beginning of the range with the help of absolute references, while keeping the end of the range relative.
There is one peculiarity to consider: the Count/Sum formula is not re-calculated automatically if you set the cell color using a ribbon button in Excel, i.e. the font and fill color icons on the Home tab. However, there is a way to go around this issue, please see the following comment:
https://www.ablebits.com/office-addins-blog/count-sum-by-color-excel/#comment-52201
This was super helpful...worked with no problem..great Macro! Glad I found this site..You saved me a ton of time working in Excel 2011 on a Mac..I would have been so lost trying to figure out how to count the cells in my worksheet by color! Thank you!
Hi,
I am trying to count by colour, but within the context of an "IF" formula.
I have a spreadsheet where I have classes which, when taught(as opposed to canceled) are filled in with the date the class took place. The row is filled in manually (entire row from left) by colour, coded to the teacher name. When a class is covered by another teacher the background colour is changed to the appropriate colour.
From this, I have another sheet which calculates how often within a specified time range (one month for billing purposes) the class has taken place. This works fine, however, I would like to find out how many classes a given teacher has taught in a specified time range. I can simply go through the sheet and count, but I would like something to count the cells by colour within an "if" function to qualify which dates to look at, as opposed to manually choosing a section of the spreadsheet.
If you are able to help with this, I would greatly appreciate it. If you would like to look at the sheet for a better understanding of what I am doing, I would be happy to send it to you.
Thank you very much!
Sincerely,
K. MacPherson
Hello,
Thank you for your comment.
For us to be able to help you better, please send a small sample table with your data in Excel and the result you expect to get to support@ablebits.com
The code is not saved after exit
tanks.
Hi, have a problem with these programms, it just dont work on conditional formating...how can fix it to work fine?
Hello, Hugo,
Please specify the programs you are experiencing difficulties with. What steps do you follow to accomplish the task? What result do you expect to get? It would be really helpful if you could send us a small sample table with your data in Excel to support@ablebits.
Hi,
This code is not working along with Conditional Formatting.
Pls suggest the alternate solution.
BR//Deepak
Hello, Deepak,
Please have a look at this article, it has VBA that works for Conditional Formatting:
http://www.get-digital-help.com/2013/10/02/counting-conditionally-formatted-cells-vba/
the below code gives error in the first row
Function WbkCountCellsByColor(cellRefColor As Range)
Dim vWbkRes
Dim wshCurrent As Worksheet
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
vWbkRes = 0
For Each wshCurrent In Worksheets
wshCurrent.Activate
vWbkRes = vWbkRes + CountCellsByColor(wshCurrent.UsedRange, cellRefColor)
Next
Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic
WbkCountCellsByColor = vWbkRes
End Function
Function WbkSumCellsByColor(cellRefColor As Range)
Dim vWbkRes
Dim wshCurrent As Worksheet
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
vWbkRes = 0
For Each wshCurrent In Worksheets
wshCurrent.Activate
vWbkRes = vWbkRes + SumCellsByColor(wshCurrent.UsedRange, cellRefColor)
Next
Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic
WbkSumCellsByColor = vWbkRes
End Function
Hello, Manssoor,
Looks like you haven't added the functions CountCellsByColor and SumCellsByColor before adding the code above. Please add these functions from the code in step 3.
Thanks, this worked great.
This is really great work. For me it worked like a charm.
Hi there,
Thank you so much for the code that allows me to sum cells by colour.
I was wondering - is there a way for me to alter the code to allow me to sum cells that are any colour OTHER THAN a specified colour?
That is, if I have say, a worksheet that has cells of 6 different colours - red, blue, yellow, purple, green and orange - can I tell excel to sum all cells that are NOT YELLOW? I would like cells that aren't coloured to be included (i.e. for excel to sum all cells that are red, blue, purple, green, orange and without fill on the basis that they are all NOT YELLOW).
Thank you for your time.
For the record, simply putting "" ahead of the colour code in the formula does not appear to work.
Sorry! "" should have read as
Hello, Katherine,
Please in the code where you find If indRefColor = cellCurrent.Interior.Color Then, replace "=" with "<>". Like If indRefColor <> cellCurrent.Interior.Color Then, If indRefColor <> cellCurrent.Font.Color Then
Hope this helps.
hi, it is very helpful VB but it has a little problem, when it is working if we change the cell color (which had had the color) it does not work until we click the sum cell hand press enter .
Hello, Reza,
This is the standard Excel behaviour. It doesn't see the change in colour as change in data. Recalculation will work if data will be changed in at least one cell in the calculated range or if you run Formulas -> Calculate Now.
I Have a doubt, if we can make an attendence with extra allowance sheet of workers and if absent days can charged a fine of $1 and multiplies the fine if absents occur in the same month as $2,$4 etc.
Hello, Linto,
For us to be able to help you, please send a small sample table with your data in Excel and include the result you need to get to support@ablebits.com. Thank you.
this was so helpful :D thank you!
Hi, I have a doubt!. If I will have a range for example "$A$1:$A$10", and:
A1(color black), A2(color blue), A3, (color yellow), A4 (color white), A5 (color white), A6 (color yellow), A7 (color white), A8 (color white), A9 (color blue), A10 (color yellow).
How I can add some cells of the same color with a macro, for example:
A1 = SUM(A2,A9) , A3 = SUM(A4,A5) , A6 = SUM(A7,A8) , A9 = SUM(A10).
Hello,
For us to be able to help you, please send a small sample table with your data in Excel and include the result you need to get to support@ablebits.com. Thank you.
The formula doesn't seem to work well with Google Spreadsheet. There are times that I simply have to refresh and voila, it will work just the way I need it to but now, I've tried several times and it just won't work.
I used this formula.
=COUNTA(valuesbyCellColor("color", range))
Hi Emmy,
This function is written for the desktop versions of Microsoft Excel, the code won't work with Google Spreadsheets.
The formula doesn't seem to work well with Good Spreadsheet. There are times that I simply have to refresh and voila, it will work just the way I need it to but now, I've tried several times and it just won't work.
I used this formula.
=COUNTA(valuesbyCellColor("color", range))
Very interesting segment on how to count by color and sum cells colored using conditional formatting. I'm also impressed with the great Q&A on this site.
Here's my question: rather than design a macro that creates a message box with the count, how could I modify the code to enter the number of the count in a particular cell? For example, when using Excel 2010, if I have a row of data that contains three cells which are red due to conditional formatting, how can I modify the macro to always enter the count of these red cells into cell A8? Thanks in advance for your time!
Outstanding functions. Thank you!
Hi,
the code is great it's very good work.
but i have a small problem, how can i make it work on network.
i have a worksheet and i need all computers in my office open it and find this macro working, when i try to open this worksheet in other computer i find the code be 'C:\Users\moataz.abdelrady\AppData\Roaming\Microsoft\AddIns\Count cells by color.xlam'!CountCellsByColor(G4:AJ4,AP4).
thanks!!
Moataz.
This code works great! Thank you!
Is there a way to extend the code to allow the conditional color to be in another cell?
Rather than the color being in the cell of the number to be added, to have another cell on the same row have the color to be checked and then the corresponding number on the same row would be added to the sum.
Does that make sense?
The function call would be something like this:
SumCellsByAnotherColor(, , )
The range added by "REF COLOR RANGE" would be a column where the cells have different colors. So, say, some of them are blue. I'd want all the numbers in "number range" to be added only if the corresponding "REF COLOR RANGE" cell was blue on the same row.
Thanks!!!
Matt
Sorry, it didn't like my greater-than less-than symbols in my post.
SumCellsByAnotherColor(numberRange, refColor, REF COLOR RANGE)
I need to have red green amber indications on one my cell based on the yes no ,NA answer from 10 column result . What formula should i right for eg I need the result on cell A1. Value to be identitified is B1=yes ,C1=NA,D1=NA,E1=NA,F1=Pending ,G1=NA,H1=NA,I1=Yes,J1=YES. Could you please advice how should I write the formula.As you are aware if yes,NA and 1 no (pending) it should be amber ,if all are no then it should be red and If yes and NA it should be Green.Please advice at the earliest
damet garm means thank you buddy
if you did so plz notify me
Sir,
i need vba code and farmula if in column 1 for different list of name and column 2 for red color so how i cant count trough name in column 1 because there is no color
plz help me
Thank You
Hi Svetlana
The conditional formatting count / sum by colour has worked for me, thankyou very much
Is there a way to have the sum total show in a cell on the worksheet?
Regards
Kimberley
Hi the code for counting by coloured cells works perfectly. But only once. Each time I colour a new cell I have to re-enter the code. Is there any way I can get the code to update automatically. Please not I have tried the fix as described in response to comment 44.
Please also note that I am VERY new to Macros and VBA
Thanks
Hello everyone,
I added a little bit to the code of one of the functions, the CountCellsByColor function. I added an If, Then statement so that the function would ignore hidden rows. Here it is, in case it would be useful to another user.
___________________________________________________________________________
Function CountCellsByColor(rData As Range, cellRefColor As Range) As Long
Dim indRefColor As Long
Dim cellCurrent As Range
Dim cntRes As Long
Application.Volatile
cntRes = 0
indRefColor = cellRefColor.Cells(1, 1).Interior.Color
For Each cellCurrent In rData
If indRefColor = cellCurrent.Interior.Color Then
If cellCurrent.Rows.Hidden = False Then
cntRes = cntRes + 1
End If
End If
Next cellCurrent
CountCellsByColor = cntRes
End Function
It's possible use the function with a named range?
Hi Cristhian,
Yes, it works with named ranges.
Thanks for the wonderful code, really appreciate the beautiful code.
Just wanted to know the method to select multiple columns of data to calculate sum by color.
For example,
I need to consider data of column J, L, and N.
Hence please let me know the corrections to be done in the formula.
Thanks.
The =CountCellsByColor worked perfectly. Thanks very much!
Every time I input the CountCellsByColor function I get a compile error "For Each control variable must be Variant or Object"
Hi, THank you for posting this - I've not been able to find the right UDF to count the conditional formatted cell colors. I'm new to creating UDF in the VBA editor and wanted to doublecheck - Do I need to paste the FIRST code and THEN the 2nd code directly beneath to get the full conditional formatting color count ulitization?
thanks!
Hi Meg,
To count and sum conditionally formatted cells, you don't need the first code, only the second one.
Also, please pay attention that the second code is a macro (the first one is a UDF), and both work in modern versions of Excel 2010, 2013 and Excel 2016 only.
Svetlana Cheusheva,
Thank you for the article. It was really helpful and I do understand it all. Although, the worksheet/workbook I build utilizes conditional formatting "Formula is equal to" with relative reference not absolute and therefore returns the same value for the cells interior color even though they visually look different. I would like to count the number of cells that are equal to a particular color but after reading: http://www.cpearson.com/excel/cfcolors.htm
the ActivCondition function will not work. Is there another way this can be done? I can provide copy of the worksheet/workbook if need be. Thank you for your help.
What a waste of time. It doesn't work.
Hello there! In need of some help... Can I combine the countcellsbycolor function with a countifs function? I would like to only count if a cell as a certain text and is colored. Any help?
Hi Cristina,
To count cells with several conditions including the cell's color, you will need to use the GetCellColor function as part of an array SUM formula. Please see my response to comment 89 for a formula example.
Thanks. This was exactly what I needed in terms of utility, explanation, and how-to steps. I very much appreciate this.
Sincerely,
Tom
Hi there! I have a color-coded table and need help. The table has a person listed for each row and various categories for the columns. I need to count how many people have a green-colored cell in any column next to their name, but if they have more that one green it should only count once. Additionally, if they have at least one green cell in their row, then these people should be ignored when tallying the number of people with at least one blue or yellow in their row . How would I do this?
Thanks a Lot it helped me
After everything we have done....wen i run the coding it says compile error : syntax error....y so..?
You guys are friggin' awesome! Thank you for posting this and the related "Change the row color based on cell value" article. So helpful.
Please help. I have different sheets with staff name verticl and horizontal 31 colomns and cells in all different colors, I want to create a final sheet that will count colors