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 26. Total comments: 838
Thank you for adding the code in response to Connor's comment about the entire workbook. I am trying to get the sum, not the count for the workbook. I just want to make sure that code works for the sum as well as the count, and if so, what data range would I use? Thank you.
Hi Josh,
It's a very good question! It actually made me realize that the formulas we published in that section were not true.
When using the function that counts and sums colored cells across all the sheets in a workbook, you need to use the following formulas to count and sum, respectively:
=WbkCountCellsByColor(A1) and =WbkSumCellsByColor(A1)
We've slightly modified the code and does work for the sum as well as for the count now. Just enter the second formula in any empty cell on any sheet without defining a range, specify the address of any cell of the needed color (it's (A1) in the example above) and the formula will display the sum of all the cells shaded in this color in your workbook.
Thanks again for your comment and your great question!
Hi,
Thanks for the code. It's very useful. I am looking for code that counts coloured cells and then recounts when a filter is applied to those cells. i.e. it counts all the coloured cells in the filtered data. Any help with this would be much appreciated.
Matt
Hi Matt,
I think we might be able to help. Please specify which exactly code you need – the first one that works with cells colored manually or the other one that supports conditional formatting.
Also, do you use Excel's auto filter or do you filter your data in some other way?
Thanks for the response.
Coloured manually - The data was originally conditionally formatted however I copied and pasted it to remove the conditional formatting so the data is effectively coloured manually.
I am using Excels autofilter.
Matt
Matt,
Try to find the following line in the code:
If indRefColor = cellCurrent.Interior.ColorIndex Then
And replace it with:
If (indRefColor = cellCurrent.Interior.ColorIndex) AND (False = cellCurrent.Rows.Hidden) Then
This line is used both in "Function CountCellsByColor" and "Function SumCellsByColor". You can change it in both functions if you want to count and sum the filtered cells, or in the CountCellsByColor function only.
Does it work now as you want it?
Thanks. I changed it and now it works perfectly.
Find the following line
If indRefColor = cellCurrent.Interior.Color Then
And replace it with:
If (indRefColor = cellCurrent.Interior.Color) AND (False = cellCurrent.Rows.Hidden) Then
Hi Boyo,
Regrettably, we do not know a way to make only one count for the merged cells. Our developers say this is a very tricky with a lot of pitfalls.
Hi Svetlana,
The code works amazingly and I thank you.
Could you please assist me in with a code of how to count merged colored cells?
Doesn't work. A window oppens giving the information of error in formula formatting. Any body could help?
Thanks
Hi Francisco,
What Excel version do you use? Please note that the script works with Excel 2010 and 2013 only.
Hello, thanks for the sumifcolor~I am manually adjusting the colors after I have entered the formulas, but now the formula will not automatically update. I have verified the "automatic" update is checked...verified other formulas are updating automatically. However, when I add another cell to the color I want to update my sum, it does not do so unless I click on the formula and hit enter.
Please advise...and thanks!
Hi Ian,
This is how all macros and VBA scripts work in Excel, you do need to click on the formula and press Enter after changing the colors manually. Please see my comment 10 above for more details. Thanks for your comment!
Thanks! Your site is going to make me look wicked smart. And I appreciate you.
Hi Svetlana,
Thanks for the code! It's just what I was looking for. For some reason, however, it's not counting correctly. I highlighted 42 rows: 27 black font, 15 red font and it's giving me a count of 16 when it's sent to count black font. I set up =CountCellsByFontColor(A3:A44,A1), where A3:A44 is the range and A1 is the color (black). Please let me know how to fix this. I would love to use this macro!
Hi Svetlana, I deleted the coding and started again and it works now. Must have been my error. Thanks so much!
Hi Cheryl,
It's great that you've figured it out on your own! And thank you for letting me know.
I believe I may have figured it out; it seems that the code will work for basic colors but will not work for custom colors. Thanks!
John,
I consulted Alex, our Excel specialist who actually wrote the code, and he says it should work with custom colors too, except for similar colors. BTW, which code have you tried - for cells colored manually or using conditional formatting?
Hi John,
Did you update a cell with the formula after changing the color manually (put the cursor in the cell, press F2 and Enter)? Please see comments 10 and 12 above for more details.
Hi Svetlana,
I've tried to use the code to do a count based on color; however, it doesn't seem to be counting correctly since the color I have on a table is red and I am trying to count whenever I manually update it to green (it currently is counting it regardless of what color I choose).
Hi Mulugeta,
I'm glad to know my article has been of help to you. I've posted all the code here and you can copy it to your worksheets. Thanks for your comment!
Dear Svetlana
Thank you for such interesting things.
if you have more detail in addition to excel
please send me the coding and the note.
God Bless you
Mulugeta
Regrettably, it is not possible to make one count for the merged cells. More precisely, neither I nor our Excel developers know such a formula. If you happen to find a way, please do share it here : )
could you add something like this to the code?
Sub Count_Merged()
Dim rng As Range, cell As Range, cell1 As Range
Dim lngMERGE As Long
Dim lngMERGEAREA As Long
Dim ws As Worksheet
Dim wb As Workbook
Dim vari As Variant
Set wb = ThisWorkbook
Set ws = wb.Sheets("Sheet2")
lngMERGE = 0
ws.Select
With ws
Set rng = Range(.Cells(1, 1), .Cells(100, 9))
For Each cell In rng
If cell.MergeCells = True Then
lngMERGE = lngMERGE + 1
lngMERGEAREA = lngMERGEAREA + cell.MergeArea.Count
Set cell1 = cell
'Debug.Print cell1.Address
End If
Next cell
End With
vari = lngMERGEAREA / lngMERGE
vari = lngMERGE / vari
MsgBox "Number of cells merged and counted as one instance is: " & vari
End Sub
that counts the cells where cell.merge is true and then adds the cell.mergearea.count values. then it divides the cell.mergearea.count by the cell.merge is true count AND finally divides the cell.merge is true value by the previous value.
lol
I have a problem regarding on counting of cells by color, if I merged two cells the total count will also two. Is their any additional formula to make only one count for the merged cell.. Thanks. !!!
Hi lkara,
In which Excel version did you run the script? We tested it in Excel 2013 and 2010 only and it may not work correctly in earlier versions. In you are using either 2013 or 2010, please specify your version of Windows and the localization of both Windows and Excel. Thank you!
I tried the conditional formatting VB code and get a syntax error pointing to the first line - Dim indRefColor As Long. Any suggestions ?
Hi Abdo,
We have not been able to reproduce this error. Can you please send us your workbook for testing (or any other sample workbook that reproduces the issue) and we will try to figure it out? If it is possible, please drop me a line using the contact form on my personal page.
Thank you!
Hello,
Had the same problem like Abdel - any luck with the debug?
Thank you for your tutorial.
I tried to use "How to count by color and sum cells colored using conditional formatting" but it did not work. When debugging there was an error at the line:
indRefColor = ActiveCell.DisplayFormat.Interior.ColorIndex
as the debug mentioned that indRefColor = 0
What's the error in my implementation ???
Thank you
Abdo
Hi Connor,
We have just added another VB script to the article that counts and sums colored cells across the whole workbook. Hopefully, this is what you are looking for : )
Hi
I'm trying to sum all cells with a certain background colour across a workbook. I have followed the steps above however when I enter the formula and press enter it takes me to the page with the code and says Compile error: Sub or Function not defined. Is there a tweak I need to make to the formula?
Thanks
I see Faith said: "I'm trying to sum all cells with a certain background colour across a workbook. I have followed the steps above however when I enter the formula and press enter it takes me to the page with the code and says Compile error: Sub or Function not defined. Is there a tweak I need to make to the formula?" on 12/19/2016.
Was there every any resolution to this comment as I am experiencing the same issue. Thanks!
Hey!
The VBA for counting colored cells worked wonderfully, but I need to count coloured cells across an entire workbook not just one worksheet to have an overall count. Do you know of any way I can do this?
Thanks!
Actually.... my cursor had to be in the cell with the formula for the F2 to work. I should have included that in my previous response for others to reference. Thanks!
Thank you so much, Svetlana. That worked perfectly. This function is going to be so helpful.
Hi Gwen,
This happens because of the following specificity of Microsoft Excel. Any macros, VBA scripts etc. work on the worksheet's level and functions are called with a change of a worksheet's data only. Changing the font color or cell color is not perceived as the data change by Excel. So, after coloring additional cells, simply place the cursor to any cell, hit F2 and Enter, the sum of the colored cells will get recalculated.
Hope this helps a thanks for your comment!
i am working in a company as a MIS Executive but i have some problem in vba coding can you help or can you give the some traning session of vba coding me
Sorry, we do not provide training services. But I am sure there are plenty such courses on the web.
THANK YOU, you saved my ass at work! ;)
Hi, Svetlana -
I used your script and formula for a spreadsheet at work - to sum cells of certain colors for a reconciliation. It was fantastic. However, when I color additional cells for values that my reconciliation found had been missed, the related formulas do not update to reflect the correct sum. Why is that? We are working in Excel 2013.
Thanks.
Hi RDuncan,
Did you use the script from the Count and sum colored cells (if conditional formatting is used) section? If so, can you please send us your workbook for testing? We've tested that VB script on many workbooks with conditional formatting and it has always worked. Also, which Excel version do you use?
Yes I can send it to you. It is a pretty large sheet that does many vlookups to gather the data. All data is within the spreadsheet. The tab (worksheet) I am working on is called "SD SLA (ALL)". I am trying to count all the failed (RED) cells and the non-failed (no color) cells. I am using Excel 2010. Is there an email address where I could send you the file directly?
Thank you
Ray
Hi Sam,
From your description, your task looks similar to the example described on step 5 of How to count by color. In my example I used the CountCellsByColor formula three times to count red, green and yellow cells. For example, I used =CountCellsByColor(F2:F14,A17) to count all red cells in the table. You enter this formula in some empty cell in your table where you want to output the sum, say, of all red cells and replace F2:F14 with your range of colored cells and instead of A17 enter an address of any cell colored in red. Copy the same formula in as many empty cells as many different background colors you want to count, just type a different cell address after the comma, i.e. the address of any cells with a certain background color, yellow, blue or other.
If you want to achieve something different, please let me know or even better post a screenshot so that we can exactly understand your task.
Hi, thanks for useful tutorial. I have excel data which cells contain number 1-1500 and each number colored with various background colors. I want to sum how many cells are red,yellow or blue. I've got visual basic editor and SumCellsByColor function but I get confused how to sum by color using that function in this case because likely different with your tutorial, is it? Can you help? :)
Hello Svetlana,
I really like your VB code for "How to count by color and sum by color in Excel". I just need to take it one step further. I'm trying to use the "How to count by color and sum by color in Excel" to count the number of Red (Misses Service Levels) and Green (Achieved Service Levels) by row on a Summary tab. The Summary tab is populated by 12 Monthly tabs (Jan-13, Feb-13, Mar-13 – Dec-13) via a VLOOKUP command. Once on the Summary tab, if the Service Level was Missed, a conditional format changes the fill color to RED, if the SLA is Achieved, the fill color is GREEN. Even though the Conditional Formatting shows the cell as RED or GREEN, your VB script still believes them to be NO COLOR Fill. Any thoughts?
Hello BreBre,
I'm a bit confused. Do you mean there's a misprint somewhere in the article or it's already time to move forward and think about Excel 2020? : )
What you have been demonstrating here is Excel 2020. Awesome piece of code !!!
EXCEL 2010
EXCEL 2020