Forum Discussion
How to count and sum "Condtional formatting" cells by color in Excel 2010?
- Feb 26, 2018
I came across this post being unanswered. so here is a User Defined Function in VBA
to SUM
use this 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
if you want to Count instead of SUM then use the below UDF
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
these solutions were provided to the similar question asked by other Excel users and worked for them.
For more detail here is the link for that answer
I came across this post being unanswered. so here is a User Defined Function in VBA
to SUM
use this 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
if you want to Count instead of SUM then use the below UDF
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
these solutions were provided to the similar question asked by other Excel users and worked for them.
For more detail here is the link for that answer
- Stanley220Feb 06, 2023Copper Contributor
Thank you Jamil for the code. I have tried to use it in Excel 2019 because the code is not working for me... I tried to replicate the formula to another range even in your file and the code did not work..
Thanks for reply
Stanley
- coulange13Apr 13, 2022Copper ContributorHI Jamil,
This actually helped me but I am getting an error because it is counting more of the red cells that what I have on the range. I am not sure how to figure it out. Please help - GiadaBellanJun 17, 2021Copper Contributor
Hey there Jamil
Thanks for the fantastic instructions!
I have tried to use the COUNT macro; the point would be to count how many blue cells are in every row.
I have not changed all the rules (they are not all formulas, but the first is) but I am not sure what else is not working.
At least the first four cells should give the result of 1 instead of NO-COLOR, right?
Could you please give it a look?
Thank you so much!- JamilJun 17, 2021Bronze Contributor
I looked at the file. it does not work because of two reasons, if you look at my earlier posts. the UDF has two limitations. A) the range you used in the UDF N4:AK4 and the Conditional formatting you used in for this range is not the same. there are multiple conditional formatting with different dimensions in in this range. B) the formulas that set the conditions of UDF are not set using formula but the "Format Cells that only contain" the UDF only works if formulas are set using "Use a formula to determine which cell to format". there are multiple conditions. I tried to understand the logic. perhaps built-in formula can be used instead of the UDF to return the same result you are looking for.
- GiadaBellanJun 17, 2021Copper Contributor
Jamilthank you for the swift response!
I did read a few of the posts, but after a few pages my eyes crossed, thank you for taking the time for explaining it anyway.
The tool has the objective of counting how many tasks are running at the same time every day.
Given each date a task with a different 'day duration' value, you can see on the right which ones overlap with each other's dates.
That is why I have so many conditional formatting rules.
My goal was then calculate how many colored cells were there in every row to see the number of tasks running.
I am not sure if I can write a formula that accommodates every condition needed.
I am still relatively new to Excel, and I am open to suggestions if you can envision a better/simpler way to realize this.
Thank you again for your insight!
- Boone_GohFeb 21, 2021Copper Contributor
I have followed the UDF but it still could not work. Can you please help?
- AECRoyJan 19, 2021Copper Contributor
=@COUNTConditionColorCells working on 1st row of table and for table total, but returns incorrect values for subsequent rows. Can you help?
Actual values in cells(U22:U25). Calculated values in cells (B33:B40).
Spreadsheet attached
- JamilJan 19, 2021Bronze Contributor
the CF range was not consistent with the range used in the UDF. I modified in the CF applied range and it works now. please see attached.
- SteveMMMay 01, 2021Copper Contributor
The background colors on cells $D$3:$F$5, a named range called COLORS2, have been applied using Conditional Formatting based on criteria met using values in columns B and C.
For instance, a value of 9 on 29-Apr is assigned a background color of "Green" (Hex #92D050) because it fell between the range of 8.5 and 10.5 per rules of item A.
The other values below and above that range were assigned "Red" (Hex #FF0000).
If either values equaled the range boundaries 8.5 or 10.5, they would be assigned "Orange" (Hex #FFC000), while blanks would not be assigned a background color and therefore should not be counted.
The CF formulas assigned are as follows:
Blank ISBLANK(D8)
Green AND(D8>$B8,D8<$C8)
Orange OR(D8=$B8,D8=$C8)
Red AND(NOT(ISBLANK(D8)),OR(D8<$B8,D8>$C8))I could not find a clean way to arrive at a COUNT for the expected values for each row (not aggregated) based on the conditions described (see attached sample). Can this be accomplished without VB scripts? If not, please provide one including a step-by-step explanation of how to use it specifically for this type of request.
- AfsarulbdNov 10, 2020Copper Contributor
Hi! Hopefully you are doin well.
I used below UDF at my workings , But outcome is not coming as expected.
Please need your kind help on this issue.
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
- Clint00Jul 01, 2020Copper ContributorHi, Does this work on 2019 excel?
- JamilJul 02, 2020Bronze ContributorIt works in any version of excel
- scott_terry87Jun 25, 2020Copper Contributor
Jamil
Hi Jamil,
Your posts are very well written and informative, many thanks for sharing your knowledge.
I am however struggling to apply the same code and logic to work on my sheet.
Please refer to the attached document, within the work sheet please go to the 'Labour forecast' tab and see that within column G (Cell G15 to be precise) I am trying to get it to count the coloured cells by condition formatting in the corresponding row.
I am getting 'Name' error return despite having the work book saved as a macro enabled sheet.
any help would be great.much appreicated.
Thanks you
- JamilJun 25, 2020Bronze Contributor
Thanks.
I looked at the file you shared. the reason you are getting NAME error, because you have many UDFs in the workbook, but none of them were the one I shared in this thread. which is this one
Function COUNTConditionColorCells(CellsRange As Range, ColorRng As Range) Application.Volatile 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).Font.Color = ColorRng.Font.Color 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
I noticed overlapped conditional formatting rules, as well as inconsistency of ranges used in UDF versus ranges used in the conditional formatting. So, even if you place the above UDF in the workbook. it will not work, because of the aforementioned issue of inconsistent range and overlapping CF.
I thought maybe you are overkilling this by using UDF, whereas the solution you are looking for can be achieved by using built in functions.
I rebuild the CF and placed some formulas in the cells O to QG and formatted those cells as custom format ;;; which shows nothing, while there is a value in it.
then applied a CF that if any of those cells hold value of 1 then show green. also those cells has nested IF formula to return 1 if the column E is not blank and is greater than row 14. then in column G, I have used a COUNTIF formula to count if row of O to QG hold a value of 1.
Please see attached and let me know if it works.
- scott_terry87Jun 25, 2020Copper Contributor
Many thanks for the response and taking the time to help me. If only more people were so kind
.
I agree there were certainly some conflicts within the code and CF rules, however I believe the fundamentals of what I am trying to achieve has been lost and subsequently the problem remains unsolved.
please see attached sheet with some additional comments.
Hopefully it all makes sense, and thank you again.
For your ease please remember it is within Labour forecast tab, and please note that the it has a 'X&Y' axis frozen pane.