How to sum and count cells by color in Excel

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
Function CountCellsByColor(data_range As Range, cell_color As Range) As Long Dim indRefColor As Long Dim cellCurrent As Range Dim cntRes As Long Application.Volatile cntRes = 0 indRefColor = cell_color.Cells(1, 1).Interior.Color For Each cellCurrent In data_range If indRefColor = cellCurrent.Interior.Color Then cntRes = cntRes + 1 End If Next cellCurrent CountCellsByColor = cntRes End Function Function CountCellsByFontColor(data_range As Range, font_color As Range) As Long Dim indRefColor As Long Dim cellCurrent As Range Dim cntRes As Long Application.Volatile cntRes = 0 indRefColor = font_color.Cells(1, 1).Font.Color For Each cellCurrent In data_range If indRefColor = cellCurrent.Font.Color Then cntRes = cntRes + 1 End If Next cellCurrent CountCellsByFontColor = cntRes End Function

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:

CountCellsByColor(data_range, cell_color)

Where:

  • 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:

  1. Insert the code of the CountCellsByColor function in your workbook.
  2. In a cell where you want the result to appear, start typing the formula: =CountCellsByColor(
  3. For the first argument, enter the range in which you want to count colored cells.
  4. For the second argument, supply the cell with the target color.
  5. Press the Enterkey. Done!

For example, to find out how many cells in range B3:F24 have the same color as H3, the formula is:

=CountCellsByColor(B3:F24, H3)

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:
Counting cells by color in Excel

Count cells by font color

In case your cell values have different font colors, you can count them using this function:

CountCellsByFontColor(data_range, font_color)

Where:

  • 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)
Custom function to count cells by font color in Excel

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
Function SumCellsByColor(data_range As Range, cell_color As Range) Dim indRefColor As Long Dim cellCurrent As Range Dim sumRes Application.Volatile sumRes = 0 indRefColor = cell_color.Cells(1, 1).Interior.Color For Each cellCurrent In data_range If indRefColor = cellCurrent.Interior.Color Then sumRes = WorksheetFunction.Sum(cellCurrent, sumRes) End If Next cellCurrent SumCellsByColor = sumRes End Function Function SumCellsByFontColor(data_range As Range, font_color As Range) Dim indRefColor As Long Dim cellCurrent As Range Dim sumRes Application.Volatile sumRes = 0 indRefColor = font_color.Cells(1, 1).Font.Color For Each cellCurrent In data_range If indRefColor = cellCurrent.Font.Color Then sumRes = WorksheetFunction.Sum(cellCurrent, sumRes) End If Next cellCurrent SumCellsByFontColor = sumRes End Function

Sum values by cell color

To sum by fill color in Excel, this is function to use:

SumCellsByColor(data_range, cell_color)

Where:

  • 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 fill color in Excel.

Sum values by font color

To sum numeric values with a specific font color, use this function:

SumCellsByFontColor(data_range, font_color)

Where:

  • 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)
Sum values by font color in Excel.

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
Function WbkCountByColor(cell_color 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, cell_color) Next Application.ScreenUpdating = True Application.Calculation = xlCalculationAutomatic WbkCountByColor = vWbkRes End Function Function WbkSumByColor(cell_color 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, cell_color) Next Application.ScreenUpdating = True Application.Calculation = xlCalculationAutomatic WbkSumByColor = vWbkRes End Function

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:

WbkCountByColor(cell_color)

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:

=WbkCountByColor(A1)

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:

WbkSumByColor(cell_color)

Assuming the target color is in cell B1, the formula takes this form:

=WbkSumByColor(B1)

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.
Sub SumCountByConditionalFormat() Dim indRefColor As Long Dim cellsColorSample As Range Dim cntRes As Long Dim sumRes Dim cntCells As Long Dim indCurCell As Long On Error Resume Next cntRes = 0 sumRes = 0 cntCells = Selection.CountLarge Set cellsColorSample = Application.InputBox( _ "Select sample color:", "Select a cell with sample color", _ Application.Selection.Address, Type:=8) If Not (cellsColorSample Is Nothing) Then indRefColor = cellsColorSample.Cells(1, 1).DisplayFormat.Interior.Color For indCurCell = 1 To (cntCells) If indRefColor = Selection(indCurCell).DisplayFormat.Interior.Color Then cntRes = cntRes + 1 sumRes = WorksheetFunction.Sum(Selection(indCurCell), sumRes) End If Next MsgBox "Count=" & cntRes & vbCrLf & "Sum= " & sumRes & vbCrLf & vbCrLf & _ "Color=" & Left("000000", 6 - Len(Hex(indRefColor))) & _ Hex(indRefColor) & vbCrLf, , "Count & Sum by Conditional Format color" End If End Sub

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:

  1. Select one or more ranges where you want to count and sum colored cells. Make sure the selected range(s) contains numerical data.
  2. Press Alt + F8, select the SumCountByConditionalFormat macro in the list, and click Run.
  3. A small dialog box will pop asking you to select a cell with the sample color. Do this and click OK.

VBA macro to count and sum conditionally formatted cells

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.

A count and sum of conditional formats

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
Function GetCellColor(cell_ref As Range) Dim indRow, indColumn As Long Dim arResults() Application.Volatile If cell_ref Is Nothing Then Set cell_ref = Application.ThisCell End If If cell_ref.Count > 1 Then ReDim arResults(1 To cell_ref.Rows.Count, 1 To cell_ref.Columns.Count) For indRow = 1 To cell_ref.Rows.Count For indColumn = 1 To cell_ref.Columns.Count arResults(indRow, indColumn) = cell_ref(indRow, indColumn).Interior.Color Next Next GetCellColor = arResults Else GetCellColor = cell_ref.Interior.Color End If End Function Function GetFontColor(cell_ref As Range) Dim indRow, indColumn As Long Dim arResults() Application.Volatile If cell_ref Is Nothing Then Set cell_ref = Application.ThisCell End If If cell_ref.Count > 1 Then ReDim arResults(1 To cell_ref.Rows.Count, 1 To cell_ref.Columns.Count) For indRow = 1 To cell_ref.Rows.Count For indColumn = 1 To cell_ref.Columns.Count arResults(indRow, indColumn) = cell_ref(indRow, indColumn).Font.Color Next Next GetFontColor = arResults Else GetFontColor = cell_ref.Font.Color End If End Function

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:

GetCellColor(cell_ref)

For example, to get the color of cell A2, the formula is:

=GetCellColor(A2)

Get font color of a cell

To get a font color of a cell, use an analogous function:

GetFontColor(cell_ref)

For instance, to find the font color of cell E2, the formula is:

=GetFontColor(E2)

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.

For example:

="#"&DEC2HEX(GetCellColor(A2))

="#"&DEC2HEX(GetFontColor(E2))
Custom functions to get a cell's color in Excel

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:

  1. In your workbook, press Alt + F11 to open Visual Basic Editor.
  2. In the left pane, right-click on the workbook name, and then choose Insert > Module from the context menu.
  3. In the Code window, insert the code of the desired function(s):
  4. 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:
Ablebits Count & Sum by Color tool for Excel

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:
Calculate cells in Excel by selected color.

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.
Calculate all colored cells at once.

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.

Available downloads

Sum and count by color in Excel - examples (.xlsm file)
Ultimate Suite 14-day fully-functional version (.exe file)

791 comments

  1. Excellent code and even better follow up assistance, from what I can see above at least.

    I a using the SumCellsByColor command code to prepare a timesheet for employees working on different jobs using the colors to sum the different 'types' of time (Normal, OT, Onshore, Offshore). I have individual tabs assigned for each employee and am using a separate 'summary' tab to bring all the information together. It is easy to prepare a summary for each employee and the type of time (using your code). However I also want to track the time spent on individual job numbers (job numbers are marked as lines on each tab with the columns being dates).

    Basically, as it is possible that each job is worked on by separate employees, I want to be able to show what type of time is being spent on each job number on my 'summary' tab.

    Can you make any sense of that? I think I may have even confused myself! haha!

    Any assistance is much appreciated!

    Thanks & Regards,

    David

  2. Really a handy tool. Saved a lot of time :-)
    Thanks for sharing it!

  3. Hello,

    First off, I love this code. I am brand new to macros and this walk-through helped me out tremendously, so thank you! I would like to use this macro with another formula, if possible. Here is what I am looking for:

    I am comparing data across multiple sheets based on the color of the cells to indicate what sales stage the contract is in as well as their expiration dates. For the report I'm trying to create, I have columns in place to show which contracts will expire 120 days from today, 90 days from today, 60 days from today, and 30 days from today. The rows indicate the sales stage that the contract is in. The columns are based on an IF statement using TODAY as the volatile factor and the rows are based on the lovely macro you have created, using each cell color as the indicator of which specific sales stage the contract is in. Since the first row indicates that the contract is in a Not Contacted state, I would like to have each row below that not only count up the number of cells that have a specific expiration date and background color, but also subtract from the Not Contacted row.

    I feel like this would require an IF statement within the macro, however I am so new to macros that I am not really sure where to begin with this one. If my explanation is confusing at all, I apologize. Any and all help is appreciated!

    Thanks,
    Antionette

  4. I can't get the macro to show when I press Alt+F8. I copied the code multiple times.

  5. This code has worked wonderfully.

    Thank you!

  6. This code was a god send... It worked wonderfully and has sped up the time it takes to complete a report I have to do each month by at least 50%

    Thanks :)

  7. This code has worked wonderfully, thank you! I only have one issue. When I saved the workbook, I saved it as a macro-enabled workbook as you instructed. I have opened the spreadsheet, and the macros are enabled because the formulas all work correctly. However...

    There is a graph in my workbook, and now - when I click on the chart - it has an error that says "Cannot run the macro "name of chart_Click'. The macro may not be available in this workbook or all macros may be disabled." Do you know why I would get this error?

    Thank you!

  8. Thank you for this code! It is great. I see it only works when I have the macro open even though I saved it as you advised. Is there anyway to make this a permanent function so that I won't have to open the macro every time?
    thanks!

  9. hello there, wondering if I can get some assistance. I want to be able to add all "points" from each location in one colum, however, each month the place of each location will very, so i want to be able to get a formula where it can be added no matter where the location number is at.

    this is kind of an example of data am working with.

    Location Quota Points Location Quota Points LocationPoints
    4014 141.00% 20 4006 46.00% 20 4006
    4022 141.00% 16 4016 44.00% 16 4016
    4003 130.00% 15 4022 43.00% 15 4003
    4016 127.00% 14 4014 42.00% 14 4014
    4006 126.00% 13 4003 41.00% 13 4022

    is there a way to figure add this up with out having to arrange them in least to greater?

    • Hello Fabiola,

      It is difficult to understand your task because your data are distorted in the comment. If you can send us your workbook at support@ablebits.com and include the result you want to achieve, we'll try to work our a proper formula.

  10. Hi, I used the VBA code and formula to count colored cells. I have 6 colors. Four of them calculate correctly, but 2 do not (extra cells are counted). What can I do to correct the error?

    Thank you.

    • Hi Kayla,

      It is difficult to say why this happens without seeing your data. Please send your workbook at support@ablebits.com and we'll look into the issue. Also, please check if the specified range doesn't include the cell that you chose as a color sample.

      • Thank you, I've just emailed Support.

        • Hi Kayla,

          Thank you for your worksheet.

          The point is that your worksheet contains some merged cells, and our formulas count each individual cell that was merged.

  11. I just tried this macro and it works well. thanks you

  12. Is there a way to count cells of a specific color and containing a specific text? Say the name Bill appears on a sheet 10 times and 5 of the cells have been manually colored red. Is there a way to count how many times cells containing the text "Bill" and background filled red?

    • =COUNTIFS(A1:L326, A330, A1:L326, getcellcolor(A330))

      Tried this and got "0" when, in fact there were 2 that met the criteria.

      A330 is a text value of a person's name as well as being filled with the reference color. I have tried it with "text value of 330" and "=text value of A330".

    • Hi Brian,

      Yes, this is possible now, we've updated the code of the functions. Please copy the updated code to your workbook and then enter the following array formula (remember to press Ctrl + Shift + Enter to complete it):
      =SUM((A1:B13="Bill")*(GetCellColor(A1:B13)=GetCellColor(D1)))

      Where A1:B13 is the range where you want to count the occurrence of the word "Bill", and
      D1 is the helper cell with the color sample, e.g. red.

      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.

  13. I have implemented your script and the patch to actively update the cell data and it works perfectly. When I reference that data in another cell it doesn't update the data without me forcing the update. By the way Ctrl+Alt+F9 will update the cells. No need to click in the cells to accomplish the data refresh. Is there another line I can add to the script to keep the referenced data active as well?

    • Hi Brian,

      Can you please send your sample workbook to Alex (alex@ablebits.com) and describe the use case step-by-step. In this way it will be easier for us to reproduce the scenario and suggest a fix.

  14. Great stuff! Saved me a lot of time!!

    Thanks a ton.

  15. hi

    how to protect the particular column and cell in the excel 2007
    i dont want whole sheet protect only particular cell i want

  16. Hi Svetlana,

    I thank you for the amazing tool. It is working well.
    I wanted to find out how do i count colored merged cells. I am creating a dashboard for data.
    Can you please help me.

    I thank you

    • Hi Arshad,

      We have not tested the code on merged cells, but in theory it should work properly, i.e. a merged cell shall be calculated as one cell. Does function behave differently on your data?

  17. Hi!

    Thank you for this script! I had problems when I tried to use it on Excel 2010. Excel said that it isn't valid formula and it has some errors.

    But then I replaced "," --> ";" and it worked (like this: =SumCellsByColor(D2:D14;A17).

    • Hi!

      Most likely this is because you have the List Separator set to ";" in your Windows Regional Settings, so you should use a semicolon in all of your Excel formulas.

      • I think it would be quite beneficial to mention this comma vs. semicolon option in the tutorial. It would have saved me a lot of time.. Thanks for the functions anyway, now that I got them working by replacing "," with ";" :)

  18. Just to follow up on my previous comment. I had already found http://www.get-digital-help.com/2013/10/02/counting-conditionally-formatted-cells-vba/ which works great in their example. We have pretty complicated conditional formatting on multiple cells. If I choose more than a row or a column or have a cell in the range that does not have the conditional formatting, I get a #Value. I do not really understand why theirs works as a function, but in a limited fashion, and yours works all the time as a sub...

    • Hello Shelli,

      I have not tested their code, but it looks like it works only with conditional formatting rules based on formulas that changed the cell's background color, if true. I am not sure it will work with color scales. This code should also be checked with regard to min/max/duplicates.

      Also, the code implies that each conditional formatting rule found in the selected range applies to ALL selected cells. If it is not so, then the code will return #Value or even throw some VBA execution error.

  19. So confused about the "How to count by color and sum cells colored using conditional formatting" needing to be a sub and not a function. Not sure I understand what the limitation is. I am still playing with it. Works perfectly as a sub but need to get the count in a cell.

    • Hello Shelli,

      Regrettably, there is no easy way to implement this as a function. I mean the function that would correctly handle all possible variations of conditional formatting. This would require a great deal of VBA code, tedious debugging and testing against different Excel versions and various Office localizations. (That is why the other solution you've tried works in simple scenarios and does not work with complex conditional formatting).

      So, our Excel specialists have weighed the pros and cons and created the Count & Sum add-in, which caters for all possible scenarios and works correctly with all Excel versions and all kinds of conditional formatting. You can download the evaluation version and try it out on your data.

  20. Great stuff! Exactly what I was looking for...

    Thank you so much!

  21. Many thanks for this article. The instructions, code and functionality were excellent and exactly what I was looking for.

  22. Or better still - how can I build a sumif formula around coloured cells. For example =sumif(range, [CellColour], [sum_range])?

    • You can use the SumCellsByColor function to sum the values of cells of a given color in the selected range. Or, are you looking for something different?

      • Hi Svetlana,

        Yes, looking for something a bit different, the cells that contain the values I want to sum are in a different row to the coloured cells so would be looking to do a calculation similar to the sumif function

        Cheers

        • Hi Wazza,

          Thank you for the clarification, I understand now. Regrettably, this is not possible because our functions do not support array formulas.

  23. This is genius. However, would vlookup and hlookup by cell colour be possible. For example, =vlookup([CellColour],table_array, column_index_num, [range_lookup])?

    • Hello Wazza,

      You need to add only one function to your workbook – GetCellColor. Then add an additional column to your lookup table, this should be the first, i.e. left-most column, and copy the following formula to it: =GetCellColor(F2). F2 is the needed cell color.

      An then, you use this formula =vlookup(GetCellColor(A1).... where A1 is the color-coded cell.

  24. Thanks for the prompt response. It worked. Hopefully, the add in that you / Aex is developing will work in both scenarios. Keep up the good work guys. Thanks

    • Hi Samir,

      Our new add-in, Count & Sum by Color, is finished and available for download. And yes, it works in both scenarios and in all versions of Excel!

  25. sorry - im new to excel :|

    here's an example - i wanted it to count only "red" when Number is filtered to "1"

    A B C D Number
    red green orange 1
    orange red green 2
    orange red green 3
    red green orange 1
    orange red green 2
    red green orange 3

    i believe this can be achieved by selecting the range manually but is there a way to do this without doing it manually?

    • Hi Dniens,

      Our code counts all cells in the selected range, including filtered and hidden cells. To be able to tell you with certainty if we can help you achieve the result you want, we need to see your data. If you can send us (alex@ablebits.com) your sample workbook and the desired result,we will try to help.

  26. This helped me a lot.. thanks..

    just one more thing on "How to count by color and sum cells colored using conditional formatting" - is there a way this can only count the filtered values? (i.e.. just count the QTY only for the PAST DUE on the Delivery column)

    • here's an example.. when i filter "1" it still counts the hidden cells... i just want it to only count "red" in number "1"

      A B C D Number
      red green orange 1
      orange red green 2
      orange red green 3
      red green orange 1
      orange red green 2
      red green orange 3

  27. Thanks for the Article -
    The spreadsheet I have contains both manual and conditional formatting in the same column,to provide same color - Red background color in this case - now the code that is written addresses either one of the two cases - ie either you can do a manual count or count by conditional formatting; is there a code that combines both cases in one so it can work in my situation.
    Also, do you know if there is a way to subtotal that can provide accurate count if the spreadsheet is filtered based on a certain criteria;

    Thanks for checking.
    -Samir

    • Hello Samir,

      The Conditional Formatting code also counts and sums cells colored manually. As for subtotal, sorry we do have any idea.

      • Hi Svetlana, I found your article very helpful. I am also looking for a way to subtotal the count of cells by color for sheets that require filtering. Is there any update on this? Thanks in advance!

  28. My chart did not post as set up.

    Company/Group #/Eff Date/Term Date/Jan/Feb/March/April (each is a column)

  29. I have a very large spreadsheet with several columns, see example below. Each month I need to count active Companies(color coded by location) for example; in January column, only count Companies that are blue and active, in February column only those that are blue and active, etc. Then repeat for Companies coded purple.

    I was able to use your code above to add by color, but now I need to eliminate those that have terminated.

    Company Group # Eff Date Term Date January February March April
    ---------------------------------------------------------------------------
    ABC Co. 123 1/1/10 1/1/14 $0 $0 $0 $0
    123 Co. 456 4/1/09 $20 $20 $20 $20
    D's Bar 789 2/1/07 2/1/14 $15 $0 $0 $0
    PJ's Pub 654 1/1/14 $45 $45 $45 $45
    Pizza Pub 321 1/1/13 $10 $10 $10 $10
    Day Center 852 1/1/12 3/1/14 $25 $25 $0 $0

    ABC, 123, D's Bar are Blue - therefore result in January s/b 2, February s/b 1, March s/b 1.

    Pj's Pub, Pizza Pub, Day Center are purple - therefore result in Jan s/b 3, Feb s/b 3, March s/b 2.

    Is there a way in January to countif blue and term date is after 12/31/13? Then in February countif blue and term date is after to 1/31/14?

    I need to read by date rather than $0 as sometimes commission is received even though terminated.

    Thank you in advance for any help you can provide, as I am very new at this.

      • I have a similar situation; is there any way you can publish and example of how to do this?

        In my case, the sheet isn't very big, I'm just trying to automate things as much as possible so that data can be updated when I'm not there to adjust things. I need to be able to sum all cells of a certain color that are adjacent to cells containing specific text.

        (I have a series of quantities in column A, and quarters - Q1 2016, Q2 2016, etc. - in column B. I need to sum only those quantities that occur next to a specific quarter, and are of a specific color.)

  30. Good Morning,

    Is there a way to count by background color only if it is prior to a specific date?

  31. When it brings me back to the module I inserted it highlights the very first GetCellColor in the module.

    Thanks again!

    • Hi Janis,

      Please make sure that you pasted the complete code in the module. The compile error usually indicates that some part of the code is missing. Please verify that the code starts with "Sub" and ends with " End sub". You can also download this sample workbook with the CountCellsByColor and SumtCellsByColor functions ready for use and try them on your data.

  32. Hi

    I posted the 3 modules you provided in my workbook. I saved it as a .xlsm file type. When I try to enter the formula - GetCellColor or CoutCellsBy Color - it brings me back to my modules coded with the error:

    Compile error:
    Expected:end of statement

    I don't know how to fix this.

    Thanks for the help!

  33. This code works great....I could use one additional function. I need to check and see if a cell has data in it before I count it.

    • Thank you Dennis, please let me know if I can help in any way.

  34. The result I get adds the totals of all the cells in the row that are being conditionally formatted. I have 3 different conditional formats, I understood that the count of sum function would only act on those that met the format of the cell I wanted summed or counted. What am I doing incorrectly or does the formula not act on the specific formatting of the cells in question? I can send the file to you if you'd like to view it. Thank you.

    • Hi Jerry,
      The =CountCellsByColor() and =SumCellsByColor functions count only cells of a specified color. For example, =CountCellsByColor(F2:F14,A17) will color the cells in the range F2:F14 that are of the same color as cell A17. If it does not work in this way for you, please do send us your workbook at support@ablebits.com.

  35. Hi! I'm trying to count by color and i've added the code in the worksheet and saved it as a excel macro-enabled workbook. But when i try to enter the formula i get the messege:"The formula you typed contains an error" I'm using Excel 2010, any idea what i'm doing wrong?

    • Hi Anders,

      Please let me know which formula are you using. Could you copy/paste it here, exactly as you are entering it in your spreadsheet, with all cell references?

  36. The straight count by colour worked perfectly thanks, once I'd read one of the queries and copied to a Word document first. Saves me all the pain I've had of having an extra column with a letter to match the colour and then sorting and counting from that. Cheers!

  37. Hey i love the code that counts the selected cells while using a conditional formatting. I just wanted to know is there a function that does the same thing. i have a file that constantly updates depending on the drop down selection. i want a function that counts the number of highlighted cells when the conditional format highlights them. Some type of count function that works with conditional formatting and updates based off of the drop down that i select. if you understand what i am saying.

    Thanks again for the help.

    • Hey Joe!

      Currently we know only one way to do this - using a macro. Other ways are not possible because of Excel's limitations. But we are working on this...

  38. I'm running into an issue where both the =CountCellsByFontColor(F3:F12,F3)and the =CountCellsByFontColor(G3:G12,G3)formulas return ALL of the cells with conditional formatting (standard red/yellow/green)rather than the ones matching the reference cell.

    Any idea why this could be happening. Also, I may have missed where someone else pointed this out, but your code above does not copy/paste cleanly into VB, as the line breaks aren't recognized. Takes a bit of manual tweaking. Not sure if this is a limitation of your site or something that can be fixed.

    Great solution to a common problem (if I can get it to work anyway), by the way.

    • Hello Dave,

      The CountCellsByFontColor function does not work with conditional formatting, it is purposed only for the cells colored manually. Please use this VBA code instead.

      As for the code formatting problem, most likely it is caused by a particular internet browser. Can you please specify which exactly browser you are using so that we can test on our side? And thanks a lot for your feedback!

  39. Hi,

    I am sure this is a questions for a different topic but hoping you can help? I have a list of 20 individuals and I want to create a 4 quadrant pie chart and place them in the region their scores qualify them to be in the pie. Can this be done?

    Thanks,
    Tatiana

    • Hi Tatiana,

      You can do this via a pivot chart or CountIfs function. This sample workbook demonstrates both solutions. Hopefully, this is what you are looking for.

      • How can i plot the Name within the chart?

        • Sorry, I do not know a way. You can ask this question on excelforum.com or mrexcel.com forums.

  40. maybe I cannot accomplish what I want -

    1. I have a column of numbers (cell A5 - A434) some of those cells w/in that range are colored yellow.

    2. on line A435 - I want to sum ONLY the yellow cells.

    Is your tutorial "How to count by color and sum by color in an Excel worksheet"
    the correct one to utilize?
    if so, I am having trouble with your code (when I copy/paste) - it copies red and utilizes 2 lines.

    I have copied/pasted other codes and they appear to paste as is.

    Thank you for your guidance
    Angel

    • Hi Angel,

      Yes, this script can help you to achieve the desired result. Try copying the code to Notepad first and then copy/paste from Notepad to Excel.
      Alternatively, you can copy it directly form the blog in some other browser.

      If this does not work anyway, please send your sample workbook at alexander.frolov@ablebits.com and we will figure this out.

      • Hi Svetlana,
        I got it to work. Thank you. I figured out that you did not need to run the code, just save it and then continue on with the formula.
        Thanks

    • Is it possible to SUM the amounts in a colored cell? Not how many of a particular color but for example - I have 7 yellow cells - each has different amounts in there... I want an end result.

      I was able to finally get my other problem resolved so I thank you dearly!

      • Hi Angel,

        Please use the SumCellsByColor function instead. It does exactly what you need - sums the values of colored cells.

  41. Hello,
    I have tried to cut and paste the code from your tutorial
    How to count, sum and filter cells by color in Excel 2010 and 2013.
    When I do, it shows up red in the module and utilizes 2 lines
    it does not appear like yours.

    I have cut / pasted other codes and they worked but this one doesn't. Can you tell me if Im missing something on this one?

  42. Hi Svetlana,

    I am using this this functions and its great "How to count by color and sum cells colored using conditional formatting", but i need to go an extra step and actually publish the count in a designated cell. How can i do this?

    Thanks,
    Tatiana

    • Hi Tatiana,

      This is not possible with the current script. But we are going to release the Excel add-in soon that will have this feature. Please stay tuned for the update.

  43. Hi Svetlana,
    Thanks for the above code but i guess if we have some unique data in cell along with the color code then there is no need of VBA coding as we can apply simple countif or sumif functions to count/sum the data.

    • Hi Puran,

      You are absolutely right. This code is purposed for the cases when there is no obvious data / color code relation.

  44. hello,

    thanks for the help. i am trying to use the count by conditional formatting. it is working when a cell's background is colored, but does not work when formatting is for border color.
    also, instead of the count popping up in a box, i would like to have it in another cell.
    could you please help me?

    thanks,
    shachi

    • Hello Shachi,

      All of the functions count cells by the background color only, the border color is not considered. We are not sure that counting by the border color is feasible, this requires additional research.
      As for the pop-up, it is displayed in case you color cells using conditional formatting. Otherwise, you can use the CountCellsByColor function that outputs the result directly in a cell.

  45. I am trying to do a bit of a hybrid so I can use an if function. My goal is to make it so if a user manually colors a cell red I can then run an if statement on it, when they remove the red it would no longer be true and can run a different formula. I have added and additional column that I will hide when done, I would like to have a way to use the background color in a single cell in a column give me the color index or some sort of color in column b that I could then run a test against. so if reds color index is 4 and cell A1 is red, then I would like 4 or something simular to show in column b. I am sure this is really simple but over my head. I tried adding the first part of the getcellcolor function but it returns the same color for the cell regardless of what color the background is. I would like to be able to do the same thing with font colors.

    I am using excel 2010 and 2013

    Thanks

    • OK, so I just figured out my problem and it was me, I have it working now. Is there any way to run a macro that will automatically do the f2 part? I am trying to hide the column that has my color index in it so users can't mess it up but now they can't hit f2 and calculate it.

      Thanks, brilliant script

      • Hi Brad,

        With the current (updated) version of the code, your users can press F2 on any cell, not necessarily the one with the formula.
        Moreover, you can do without F2, by using a solution I posted in response to comment 44 above. Hopefully, this will be helpful for you too.

  46. Hi,
    Svetlana, you are a genius in excel. I like your vb code.
    Thank you.

    • Thank you very much Sofi!
      But all accolades should really go to our Excel guru Alex : ) As I mentioned in the article he wrote the code for this example.

  47. Hello,

    Thank you for sharing the instructions and code. I'm experiencing some issues with the implementation. When trying to open and run the macro using function Alt F8, the macro does not appear. When I search for the Macro Enabled Workbook file (.xlsm) that I've saved, I see that there is an exclamation mark on the file icon. When attempting to open the .xlsm file, I receive the following error: "Compile Error: Invalid outside of procedure" Could you please help me to figure out what I'm doing wrong? Thank you!

    • Hi Sarah,

      Please verify whether you copied the entire code from the web-site (from our experience, occasionally the first or last line is missing). If it is not the case, try to add the code anew to some other worksheet. If the error persists, please specify what Excel version you are using.

  48. I would like to write a single IF statement to print a "1" or a "2" in the cell if the colorindex=3. I tried something like =If(range("a1").colorindex=3,1,2)) but something isnt quite correct, I could do the VB code, but i really want something really simple like the IF statement since I am only looking a single cell. I dont want to use "conditional formatting", the color of the cell is set via VB code (selection.colorindex=3)
    Thanks

    • Hi John,

      Excel's IF function as well as all other functions work with simple expressions and standard Excel formulas only. You cannot use any VBA code inside the IF statement. Also, as far as I remember none of standard Excel functions can return color index. And this was the reason why custom VBA code is needed for this task.

  49. Now, I am getting #NAME? in the boxes where my formula =CountCellsByColor(N102:N334,A335) was entered. Please Help!

    • Hi Cassy,

      It's hard to say what exactly causes the error without seeing your workbook. If possible, you can send it to us at support@ablebits.com and I'm sure we'll be able to fixed.

  50. PS.. I have saved it as macro-enabled workbook

    • Hi Khaled,

      The only possible reason that I can think of is that you accidentally added the code to a module of some other workbook. Will you check that please?

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 :)