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
Jamil Dear Mr. Jamil : I tried to use on my sheet but it give me #VALUE!, I attached the sheet. I used 2 formula ( in Cell AN2 and AN5 )for Formatting the cells . Please can you help me get the formula and show me the code.
Perhaps, you did not read my earlier posts. I have mentioned that the range used in the UDF must be a mirror of the range in the conditional formatting. I can see from your example file that you have applied multiple range in the single rule of conditional formatting and also it is vertical by column and yet the UDF is using single range of single horizontal row. so the ranges do not match that is why you get the error.
- khaled170Apr 21, 2020Copper Contributor
Jamil I already get the code running on my log sheet , but I am getting #VALUE! then If I select the formula and press Enter it will run and if I drag it below it will give correct answer only for 10 rows below and below this the formula showing #VALUE! then need from the formula in the last row which showing correct result to drag below and again it will give correct answer in the below 10 rows and the other row will show #VALUE!.
Is their any way to let the formula properly work for all the rows as I drag it below ?
Attached the file I am working with.
- JamilApr 23, 2020Bronze Contributor
I run a small test and I see that there are a lots of circular references in formulas inside conditional formatting rules. and each conditional formatting rules has multiple logical test and it overwhelm the calculation engine. Unfortunately, I do not have time to build from scratch a neat table for you, as it takes a lot of time. If I was you, I would not use nested IF inside the Conditional formatting rules and instead use helper column, this way you avoid too much calculation and circular reference issues.