Forum Discussion
sum by color when colors are set by conditional formatting
- Jan 19, 2017
Hi matt nipper,
since you mentioned that you "have done an exhaustive search online" i have come up with a solution for you, although it has two limitations A) it will only work, if your rules of conditional formatting is created using conditional formatting rules with formula aka (use a formula to determine which cells to format) and B) the UDF will only work if sum range is more than one cell another word, it will not sum a single cell, as well as the conditional formatted range is more than one cell.
the example file, you can download it from here. I could not upload it here, as it is a Excel Macro-enabled Workbook that contains the UDF
Function SumConditionColorCells(CellsRange As Range, ColorRng As Range) Dim Bambo As Boolean Dim dbw As String Dim CFCELL As Range Dim CF1 As Single Dim CF2 As Double Dim CF3 As Long Bambo = False For CF1 = 1 To CellsRange.FormatConditions.Count If CellsRange.FormatConditions(CF1).Interior.ColorIndex = ColorRng.Interior.ColorIndex Then Bambo = True Exit For End If Next CF1 CF2 = 0 CF3 = 0 If Bambo = True Then For Each CFCELL In CellsRange dbw = CFCELL.FormatConditions(CF1).Formula1 dbw = Application.ConvertFormula(dbw, xlA1, xlR1C1) dbw = Application.ConvertFormula(dbw, xlR1C1, xlA1, , ActiveCell.Resize(CellsRange.Rows.Count, CellsRange.Columns.Count).Cells(CF3 + 1)) If Evaluate(dbw) = True Then CF2 = CF2 + CFCELL.Value CF3 = CF3 + 1 Next CFCELL Else SumConditionColorCells = "NO-COLOR" Exit Function End If SumConditionColorCells = CF2 End Function
in the attached example file you can see that from A3:G16 cells are formatted using Conditional Formatting.
User Defined Function (UDF) is placed in cells J2 & J3 and cells I2 and I3 are the criteria color used as reference inside the UDF in J2 and J3.
I hope this helps you.
Edit: uploaded file and updated the code to the correct one.
Hi Jamil,
Thanks for the quick response. It definitely appears to be a rounding issue. I looked at a smaller set of data and ran some tests on it.
The data I tested was the following 6 values - 45.18, 45.26, 45.21, 45.39, 45.24, 45.41
The sum value should be 271.69, however the function is returning a sum of 270.00 (which is each value rounded to its nearest whole number - 45*6)
I then changed the first value to 45.68, at which point the function returned a sum of 271.00 (rounding the first value up to 46, and continuing to round the remaining values down to 45)
Unfortunately I'm not in a position to attach any samples, but if there is any reason that you think this issue is occurring then please let me know.
Thanks in advance.
Micheal
Hi Michael,
Ok. Can you please replace the previous UDF with this one attached below. This should fix the problem.
Please let me know how it goes.
Function SumConditionColorCells(CellsRange As Range, ColorRng As Range) Dim Bambo As Boolean Dim dbw As String Dim CFCELL As Range Dim CF1 As Single Dim CF2 As Double Dim CF3 As Long Bambo = False For CF1 = 1 To CellsRange.FormatConditions.Count If CellsRange.FormatConditions(CF1).Interior.ColorIndex = ColorRng.Interior.ColorIndex Then Bambo = True Exit For End If Next CF1 CF2 = 0 CF3 = 0 If Bambo = True Then For Each CFCELL In CellsRange dbw = CFCELL.FormatConditions(CF1).Formula1 dbw = Application.ConvertFormula(dbw, xlA1, xlR1C1) dbw = Application.ConvertFormula(dbw, xlR1C1, xlA1, , ActiveCell.Resize(CellsRange.Rows.Count, CellsRange.Columns.Count).Cells(CF3 + 1)) If Evaluate(dbw) = True Then CF2 = CF2 + CFCELL.Value CF3 = CF3 + 1 Next CFCELL Else SumConditionColorCells = "NO-COLOR" Exit Function End If SumConditionColorCells = CF2 End Function
- hiepkt12Apr 09, 2019Copper Contributor
I want to count the number of yellow boxes as in the file below and output in the column "count color yellow" Thank you so much!
- Michael CollinsMay 09, 2018Copper Contributor
Hi, this is exactly what I'm looking for but I just need count not sum. Is it easy to re-produce this as a countif?
- lihkiongJun 11, 2020Copper Contributor
I need to count by font color by conditional formatting. Pls advice how to do?
attached is the file.
Count how many cell above 7, below 3 and between 3-7
- JamilJun 15, 2020Bronze Contributor
Hi, you did not set your conditional formattings using formula. that is why it does not work.
please see an example file with the code for counting based on the font color set by conditional formatting in my answer here https://techcommunity.microsoft.com/t5/excel/sum-by-color-when-colors-are-set-by-conditional-formatting/m-p/174487/highlight/true#M8272
- JamilMay 22, 2018Bronze Contributor
Michael,
please see attached example for COUNT as requested.
- Matt McIntireSep 19, 2018Copper Contributor
Jamil - This is a great tool, however, is it possible to make it work for 3 colors? The counts and sums are inaccurate for me after the first color when there are more than 2. Thank you!
- Steve ThurstonMar 22, 2018Copper Contributor
Good evening Jamil,
Do you happen to have a version of this UDF which would sum cells with a given font color rather than an interior/background color? I have tried changing the word "Interior" to "Font" in the UDF, however when I run the formula on my desired cell, it simply shows "NO-COLOR" rather than adding the cells with the desired conditionally formatted text color. Any help would be appreciated.
Regards,
-Steve
- JamilMar 22, 2018Bronze Contributor
Hi Steve,
If you mean the font color set by conditional formatting. then you can change the line
From
If CellsRange.FormatConditions(CF1).Interior.ColorIndex = ColorRng.Interior.ColorIndex Then
ToIf CellsRange.FormatConditions(CF1).Font.Color = ColorRng.Font.Color Then
Please see it in the attached sample workbook.
- Steve ThurstonMar 23, 2018Copper Contributor
Thanks Jamil, this gets me very close. However, I suppose what I really need is for this VBA to count the number of cells within the selected range if they are a certain color, not sum the values within the cells, as the values within the selected cell range are text, not numbers. What would I change in the VBA to accommodate this?
Regards,
-Steve
- Arup Kumar DasFeb 25, 2018Copper Contributor
Is it possible for cell count instead of sum. Please share the VBA code if possible.
- JamilFeb 26, 2018Bronze Contributor
Yes, it is possible and here it goes.
Function COUNTConditionColorCells(CellsRange As Range, ColorRng As Range) Dim Bambo As Boolean Dim dbw As String Dim CFCELL As Range Dim CF1 As Single Dim CF2 As Double Dim CF3 As Long Bambo = False For CF1 = 1 To CellsRange.FormatConditions.Count If CellsRange.FormatConditions(CF1).Interior.ColorIndex = ColorRng.Interior.ColorIndex Then Bambo = True Exit For End If Next CF1 CF2 = 0 CF3 = 0 If Bambo = True Then For Each CFCELL In CellsRange dbw = CFCELL.FormatConditions(CF1).Formula1 dbw = Application.ConvertFormula(dbw, xlA1, xlR1C1) dbw = Application.ConvertFormula(dbw, xlR1C1, xlA1, , ActiveCell.Resize(CellsRange.Rows.Count, CellsRange.Columns.Count).Cells(CF3 + 1)) If Evaluate(dbw) = True Then CF2 = CF2 + 1 CF3 = CF3 + 1 Next CFCELL Else COUNTConditionColorCells = "NO-COLOR" Exit Function End If COUNTConditionColorCells = CF2 End Function
- hitmandFeb 02, 2022Copper ContributorHi Jamil
First of all, thank you for this code it has been massively helpful to me. I have an issue I am hoping that you can help me to resolve. I got the code working except for this part:
dbw = CFCELL.FormatConditions(CF1).Formula1
dbw = Application.ConvertFormula(dbw, xlA1, xlR1C1)
dbw = Application.ConvertFormula(dbw, xlR1C1, xlA1, , ActiveCell.Resize(CellsRange.Rows.Count, CellsRange.Columns.Count).Cells(CF3 + 1))
My understanding is here is where the code is counting the change in colour and updating the 'dbw' variable accordingly. I have used your example version and it works perfectly. When I run it in my workbook it doesn't work once the loop is finished I am always returned 0. If I edit the code I can get it to count all the cells that have a colour but I then have the issue that it won't differentiate between the different colours and will return me the number of all the coloured cells. Please, can you advise why this section of the code may not be working for me?
- t.aerdtsNov 28, 2017Copper ContributorHi Jamil,
I have a GANT diagram for were I want to sum the values in column cells with a specific colour.
This is to sum how many labour hours we have to make each day for a specific background color that was generated by the conditional formatting.
In this GANT diagram a pop up boxt can not be used. Do you have a solution for that?
- JamilFeb 26, 2018Bronze Contributor
Hi t.aerdts,
I think that can be achieved using Formulas with the referred UDF. If you post a sample dummy data here and I will have a look in the weekend.
- t.aerdtsFeb 26, 2018Copper Contributor
Hi Jamil,
herewith the sample file.
appreciate your input.
I put some comment in the file.
KR T.aerdts
- Micheal HillNov 08, 2017Copper Contributor
Hi Jamil,
Thanks again for the response.
I've just tried your amended code and it works perfectly. Your help on this is very much appreciated!
Best Regards,
Micheal
- JamilNov 12, 2017Bronze ContributorYou are welcome. Thanks for the feedback