Comments on: How to sum and count cells by color in Excel

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 4. Total comments: 830

  1. I need help with a formula that I'm needing for a new work spreadsheet that I'm trying to create to keep track of which employees have completed their yearly certifications. I have employee names in cells the first 25 cells of columns B through W (they are broken down in a specifc way so I can't add or combine more in a column). As each employee Right now I am using a CountA formula that I'm using showing the total of employees (= counta (B2:W25) but as I highlight the employee names when I get their confirmations I want that cell subtracted from the total so I can see at a glance how many employees are left. I read about the count by cell color formula and that would work but I can't figure out how to combine the two to get a total. In my mine I thought I could put =counta(b2:w25)-CountCellsByColor(b2:w25) and that would give me my total but it doesn't. Please help.

    Thanks in advance for any and all help!

    P.S. I have Ablebits downloaded on both my personal and work computers and it's helped me so much. Glad I downloaded and bought it.

    1. Hello! In the CountCellsByColor function, specify the second argument – Cell_color. This is a reference to the cell with the target fill color.
      For example,

      =COUNTA(B2:W25)-CountCellsByColor(B2:W25,A1)

      Hope this is what you need.

  2. Countcellsbycolor worked perfectly thanks very much. Is there a way to modify the code so it counts the number of rows of a colour within a certain range? I.e. all rows from A10:A50 that are coloured the same as B2.

    1. Hi! The first argument of the CountCellsByColor custom function is the range in which you want to count cells.

  3. How to get the totals updated when a color is changed?

  4. CountCellsByColor Function Not Working, When I use this function then result are 0, please solve this problem..

      1. I want to count individually cells. For exp- i have hugh excel data and i want to count how many green cell & how many pink cell in table, but the proble are when i used your function then result are 0 every time .
        That's means - green cells -- how many
        pink cells -- how many
        pleace help how to do.

        1. Unfortunately, I can't work with your data. Try to use the sample file linked at the end of the article. Use the CountCellsByColor function, don't forget to specify the cell with the desired color.
          To count conditionally formatted cells, use VBA macro, as described above.

  5. I modifed the code to count the celss with conditional formating as below but it is giving value error
    Function CountCellsByColor(data_range As Range, cell_color As Range) As Long
    Dim indRefColor As Long
    Dim indCurCell As Long
    Dim cellCurrent As Range
    Dim cntRes As Long
    Dim cntCells As Long

    Application.Volatile
    cntRes = 0
    cntCells = data_range.CountLarge
    indRefColor = cell_color.Cells(1, 1).DisplayFormat.Interior.Color
    For indCurCell = 1 To (cntCells)
    If indRefColor = data_range(indCurCell).DisplayFormat.Interior.Color Then
    cntRes = cntRes + 1
    End If
    Next
    CountCellsByColor = cntRes
    End Function

  6. Counting of colored cells does not work on cell ranges that are conditionally formatted.

    1. Hi! Pay attention to the following paragraph of the article above: Count and sum conditionally formatted cells.

  7. When I change a cell's color, will the totals be updated instantly?

  8. This is very helpful!

    Is there a way to combine CountCellsByColor and CountCellsByFontColor? I have a set of data that has 5 different colors and 2 different font colors and trying to get 10 unique values from it. I was thinking it would incorporate CountIfs but my tests haven't been working.

    Thanks in advance!

    1. Hi! Unfortunately, it is not possible to combine the two custom functions. A new custom function must be created.

  9. Hi, great extention and very helpful, thanks. But if I need to pick just several cells instead of a certain range I can't understand how to.
    for example instead of
    =SumCellsByColor(D18:F21;O20)
    I need to summm only four cells, like
    =SumCellsByColor(E18+F19+E20+D19;O20)
    but it doesn't work. What am I doing wrong?

    1. Hi! Use either a single cell or a range of cells in the SumCellsByColor function. For example,

      SumCellsByColor(E18)+SumCellsByColor(F19)

  10. This is great stuff! Thank you. Got a question, though. Two columns side-by-side. Each column has cells that are colored either red or green. Trying to count the cells where it's red in column 1 and green in column 2, or green in column 1 and red in column 2. Also need to count the ones that are red in both columns and green in both columns. Is there a way to do this?

    1. Hi!
      This formula can count how many cells in the both columns have a value equal to cell A2 and are colored red.

      =MIN(SUM((GetCellColor($A$2:$A$20)=255)*($A$2:$A$20=A2)), SUM((GetCellColor($B$2:$B$20)=255)*($B$2:$B$20=A2)))

      I'm not exactly imagining your data, so try the formula below as well. It compares the cells in each row in pairs.

      =SUM((GetCellColor($A$2:$A$20)=255) * (A2:A20=B2:B20) * (GetCellColor($B$2:$B$20)=255))

      Use the custom functions from the article above.

  11. The Count and sum by color across entire workbook was well explained and simple to embed into my Workbook.
    Thankyou so much!!

  12. Hello , I am using MICROSOFT OFFICE LTSC PROFESSIONAL PLUS 2021 and I follow the same formula and it still not work #NAME,
    Then I download the excel sample and copied the entire data with the formula and the result is the same #NAME

    Any help?

    1. Hi Fernando,

      From the sample workbook, you need to copy not only the data/formulas but also the function's VBA code. Have you done that?

    2. Hello!
      Have you saved the file where you added the code as a Macro-Enabled Workbook (.xlsm)? If the issue remains unresolved, please try downloading our sample workbook provided at the end of this article, unblocking the macro (Microsoft automatically blocks macros in any files downloaded from the internet), and see if the function works in that particular workbook.

  13. I have the following problem with respect to "Count on conditionally formatted cells"
    1. For my data I need to filter it first on 1st column lets say with INDEX or FILTER functions
    2. Range received as output of Step 1 needs to be used as Input of "Count on conditionally formatted cells"

    How to do this ?

    Example : lets take the same sample table as you showed but add another column before the 1st one as category
    Say choose 2 categories as "Fruit1" and "Fruit2" and place these category against each of 2nd column data

    Now I need separate "Count on conditionally formatted cells" for "Fruit1" category and separate for "Fruit2" category ...

    Pls help on how to achieve this ...

      1. hmm... I am not familiar with VBA.. Is someone could help with this ?

  14. how to add the countecellby color in excel and not run the VBA.
    thanks !

  15. Hello! First of all, I really appreciate all the tips and tools; they are extremely helpful. So here is my problem. I added the Macro for CountCellsByColor and saved. Wrote for formula in the associated cell reference and it calculated, but the number it calculates is not correct. for example, I am approved for 10 staff, I have 9 currently and one potential new hire. So, instead of it count 9, it is calculating 14, what I am doing wrong?

  16. Using CountCellsByColor and SumByColor and they work GREAT! Thank you!!
    How would I then get the MAX from the SumByColor?
    I have tried a few things and none work, so far. Unfortunately, I do not code very well...

    • Looking for a MaxByColor routine.
    Any help would be most appreciated!

  17. Hey all! Thanks so much for posting this code. Is it possible to output conditionally formatted color count to a cell so that I can sum cells? I was able to use the macro to do it perfectly, but I need to be able to separate counts and then sum for a total. Is there any way to do this?

    1. Nevermind, I found the answer below from Alexander and have used this successfully. Thank you Ablebits team for this help!!!

  18. I can't get this to work to save my life. I'm trying to get it to count cells by color, used your formula and it if throws #NAME? in the place of "CountCellsByColor".

    1. Hi Steven,

      Did you save the file where you've added the code as a Macro-Enabled Workbook (.xlsm)?

      If the error persists, please try to download our sample workbook at the end of this article, unblock the macro (by default Microsoft blocks macros in all files downloaded from the internet), and see if the function works in that workbook.

      1. Same problem. What is need to be followed in the 1st paragraph?

          1. done this but still getting the #name? error

            1. Hi!
              #NAME? error means that either you got the function name wrong, or the function is not present in your version of Excel.

              1. Having the same issue. Copied and pasted the CountCellsByColor VBA code. Saved workbook as .xlsm. Still getting the "#NAME?" error when trying to use the function.

  19. Much appreciated. I'm finding the coding her very helpful.

  20. Hello All,

    it was worked if count by number but not if it count by date. could you please help me on this.

    appreciated for your support.

  21. Hello. Thanks a lot for the codes. I am wondering whether it could be possible to use the function for conditionally formatted cells in a cell as a general function to be inserted in whatever cell (not as a macro in a dialog box). I.e. I am looking for adjusting the function for summing colored cells which would work for conditionally formatted cells as well. Any ideas about adjusting the code?
    Thanks.
    Stanley S

      1. Hi Alexander! Below, in January Svetlana Cheusheva wrote a reply that there is only a macro available for both sum&count. But directly under this comment I am writing now I already asked you about some possible way of adjusting your code for function from "count" to "sum". Is it possible?
        Thanks!
        Have a nice day,
        Stanley

        1. Hello!
          Try this code:

          Public Function SumByColorCells(CellRange As Range, TargetCell As Range)

          Dim TargetColor As Long, SumCells As Long, C As Range

          TargetColor = Evaluate("cfcolour(" & TargetCell.Address & ")")
          For Each C In CellRange
          If Evaluate("Cfcolour(" & C.Address & ")") = TargetColor Then SumCells = C + SumCells
          Next C
          SumByColorCells = SumCells
          End Function

  22. SIR,

    I AM NOT GET COUNT OF COLORS WHERE CONDITIONAL FORMATTING APPLIED BY ABOVE MENTIONED SOLUTIONS/

    KIDNLY HELP

    1. Hello!
      This custom function counts all cells shaded by a certain color. Including with conditional formatting.

      Public Function CountByColorCells(CellRange As Range, TargetCell As Range)
      Dim TargetColor As Long, Count As Long, C As Range
      TargetColor = Evaluate("cfcolour(" & TargetCell.Address & ")")
      For Each C In CellRange
      If Evaluate("Cfcolour(" & C.Address & ")") = TargetColor Then Count = Count + 1
      Next C
      CountByColorCells = Count
      End Function

      Function CFColour(Cl As Range) As Double
      CFColour = Cl.DisplayFormat.Interior.Color
      End Function

      1. Hi Alexander!
        Thank you for solving the case. Do you have any ideas on summing the range of cells including conditional formatted cells? Replacing "count" with "sum" in your code simply does not help but I do not know why...
        Thank you!
        Stanley S.

  23. I am using CountCellsByColor. Can I count cells that also contains a certain text? It counts the cell colors fine, but other users are highlighting other things in the same colors which are now giving wrong calculations. eg cells that are yellow, but also contain 'test' for example.

  24. This was really useful, I was trying to do an analysis and the way the analysis was done was by color. But I wanted to count some colors and wanted to do the "if" statement, is there a way I can do that?

    1. Hi!
      Have you tried the ways described in this blog post? If they don’t work for you, then please describe your task in detail, I’ll try to suggest a solution.

  25. Nice code.

    Appears to be a small typo

    VBA macro to count and some conditionally formatted cells.
    Sub SumCountByConditionalFormat()

    ... and SUM conditionally ...

    1. Hi Ron,

      Oops, a silly typo indeed. Fixed, thank you for pointing that out!

  26. Your code for "How to sum by color in Excel" is excellent with only one issue, it doesn't automatically recalculate if you color the cells after setting the the calcuation cell. Forcing the recalculate works well and my sheet is set to automatic recalculation but it will not. I assume closing and re-opening will also cause the recalc.

    Any help appreciated.

  27. Hello,

    I was able to use SumCountByConditionalFormat just fine as a sub however i was wondering if there is a way to make it a function? so instead of preselecting and answering a msgbox i could place the information in the formula bar?

    Thank you,

    Halle

  28. I really like the function. I am wondering If there is a way to make it refresh on color change. in other words, I use it to track tasks. So when I complete the task change the color of the cell. If I use the paintbrush it works but if I just apply the new color it does not update until I click into the cell and click out.

  29. After add this module in excel, it need to loading everytime when I edit the excel, may I know how to solve this problem?

    1. It also is no responded and blur screen loading. Is the function not suitable for my excel or did I set sth wrong?

  30. I added in your VBA for Count by color across entire workbook and saved it as a macro enabled excel file, i created a new tab and filled a cell with the colour i wanted to contain cell A1 i then used the formula =WbkCountByColor(A1) in the adjacent cell. the following error was then generated: Microsoft visual basic Compile error: Sub or function not defined. Seems to relate to text in the code CountCellsbyColor? not sure how to remedy the situation?

    Thanks

    1. Hi!
      For this function to work, the code for the CountCellsByColor and SumCellsByColor functions must also be added to Excel. Read the Note carefully after the function code.

      1. Thanks, I got it to work and finished the workbook, however after closing and reopening the file in order to get the function to work it now reads VBAProject.Module2.CountCellsByColor(b2:b8, b2)
        Any idea why this has changed as now the formulas don't work and I'll manually have to edit them all to get the functionality back?

  31. Is there a function for the macro "Count and sum conditionally formatted cells" ?

    I need the number provided as an output in a cell that I can leverage with other formulas elsewhere.

    1. Hi Eric,

      Nope. For conditionally formatted cells, we only have a macro.

  32. Great resource! I used the Sumbycolour function and it just WORKED through!

    Appreciate the good work. Thanks a lot!

  33. I can get it to work when I place the macro in the specific workbook I am working on, but cannot get it to work when I save it in the Personal WB via VBA. Any thoughts? I create new WB's from a program each week and need it to work from the personal macro wb vs having to re-add it every week to the new wb. At that point it it not useful. :(

  34. Hello! I used the CountCellsByColor VBA code and it worked like a charm. Only one issue, it is not automatically updating - I have to hit calculate now. I have made sure the Automatic Calculate option is turned on, but is not working. Any quick fix suggestions?

    Thank you!

    1. Hi!
      Pay attention to the following paragraph of the article above - How to force custom functions to update.

  35. hi,

    I have used your code in desktop successfully but when I upload into the server it shows " Security Risk - Microsoft has blocked marcos from running because the source of this file untrusted"

    Kindly assists with thanks.

      1. click file> and next move to option > trust option > trust settings> macro setting

  36. I'm using =CountCellsByColor(C4:AG4,AK3) and it did work well when I have applied all to spread sheet,but after reopening Excel workbook I got everywhere NAME ERROR. What I'm doing wrong?

  37. I also tried this, but it didn't work.
    =SUMPRODUCT(IFERROR(--(MOD($D$39:$L$47,1)=0),0),SumCellsByColor($D$39:$L$47,D9))

    Where D9 is the BASE color and
    $D$39:$L$47 is the range with different colors, integers, and decimals, which I am trying to SUM only the integers of the D9 color.

    Thank you very much for your time
    Best regards
    Drago

      1. Thank you very much for your quick response.

        Unfortunately, I get a #Value error. I copy and paste your solution, then try ctrl+shift+enter to activate it, but get a #value error.

        But we managed to do it by adding 2 lines in the macro code for the SumCellsByColor

        If IsNumeric(cellCurrent) Then

        If Int(cellCurrent) = cellCurrent Then

        Thank you so much for this article. It will save me a lot of time in the future.

        Best regards
        Drago

        p.s If someone is interested in the whole macro for this particular case, I could paste the whole code in the comment section.

  38. Hello,
    Thank you very much for this. It works like magic.
    I have a small problem though. I need to sum ONLY the integer values from the cell range and to exclude the decimal numbers.
    I have a cell range with integer and decimal numbers and I use the =SumCellsByColor($D$39:$L$47,D10) formula and I need the SUM of ONLY the whole nubers.

    Thank you very much

    Best regards
    Drago

  39. I got a problem to sum the amount in the green box. The thing is, whenever the checkbox ticked (true) then the cell contain an amount will turn to green (next to checkbox cell). I want to make the sum formula only for the green boxes for total revenue from January to December (horizontal data).

  40. Its really great using this solution.

    It resolved my problem which could have taken weeks time for me to calculate the colored cells.

    Thank You very much.

  41. Your examples above are either (1) sum or count by color, or (2) sum or count by font. They are great and helpful. But I am just wondering if there is a formula to perform [count by color + font] and [sum by color + font] ?

    Thanks

  42. Is there any way to make the cells auto-calculate when applicable cells are formatted?

    1. Hello!
      Formulas are automatically recalculated when the cell value changes, but not the format. Your problem can be solved with a VBA macro.

  43. It does wonders. But I am still not able to execute the sum part of it. The value keeps coming to 0. No problem with the count function though. I will be grateful for the help.

    1. Hi!
      I’m sorry but your task is not entirely clear to me. Could you please describe it in more detail?

  44. How can the macro can be modified if I only want to count or sum the colored cell after filter the table? My apologies, still much a novice at VBA.

  45. You are a goddess! This is incredible. Thanks! I got all the functionality to get this done.

  46. Thanks so much from Pematang Siantar City Indonesia.

  47. Worked perfect

  48. I have a table that lists all shipments not yet invoiced. I have colored all the rows using conditional formatting. As an example, shipments that are overdue for invoicing are colored red, based on more than 5 days past the arrival date of the vessel. We have several managers responsible for different types of commodities, e.g. bulk oils and petroleum, frozen foods, chemicals, etc.
    Can the CountIfs count how many reds, blues, greens, and yellows by manager? Do you have a code that can do this for me? At the moment I am counting manually.
    I have tried =CountCellsByColor(DBN!$B$6:$B$1299,SUMMARY!$C$3) but this results in total blue cells, not how many blue cells a particular manager has.

      1. Is there any VBA code for counting colored celled which are conditionally formatted?? i did not find solution anywhere for latest excel versions

        1. Hi!
          Have you tried the ways described in this blog post? Why don't you want to use the VBA code that is suggested in the first paragraph of the article?

  49. LOVE this code - thanks so much!

    In my sheet I have dates in Row 7
    And data in the rows under it

    I have a start date in Cell E4, and a end date in column d starting at D8

    I want to only sumbycolour when the dates are equal to or inbetween the 2 specified dates

    So Start Date is 12/11/21
    End Date is 16/11/21

    The row with the data i'm counting has data in it for the whole year

    I feel like I need to include an IF statement - but getting lost - appreciate anyones help!

      1. Thanks that helped heaps.... so I now have the SUMIFS working, and have it written like this:

        =SUMIFS(F5:Z5,F4:Z4,">="&B1,F4:Z4,"<="&C5)

        Where I have dates in B1 and in C5

        Is there a way to incorporate this with the sumbycolor - maybe a sumbycolorifs?

        I want the above to only sum when cells are of a specified colour.

        Thanks again

        1. Hello!
          You want to sum by both date and cell color. But these two conditions can be applied using different functions. Unfortunately, you can only combine them if you add VBA code to the SUMBYCOLOR function.
          You can filter by dates on your table and then use the SUM BY COLOR tool. It is available as a part of our Ultimate Suite for Excel that you can install in a trial mode and check how it works for free.

          1. Thanks again - unfortunately due to other restraints in the workbook / I don't have the ability to use filtering for this.

            Anyone have the knowledge / skill required to write the code required to make sum by date and cell colour work together? Would so appreciate it!

            1. Hi @Alexander Trifuntov - any chance you can help with the code for this?

              1. Hi!
                Unfortunately, we are not in the business of writing VBA code. However, the filter is a basic Excel tool. I don't really understand why you can't use it.

  50. Hi
    I tried to use the function but every time I am getting #name error. What should I do?

Post a comment



Thank you for your comment!
When posting a question, please be very clear and concise. This will help us provide a quick and relevant solution to
your query. We cannot guarantee that we will answer every question, but we'll do our best :)