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
Hi Jamil,
The example that you helped me with worked like a charm but only for one conditional format formula applied for a range of cells.
Now i have a case in which i want to count all the CF cells in a row, but with multiple CF formulas.
I followed all the steps that you suggested (i hope i didn't missed something out).
I have attached the worksheet.
Thank you in advance
Alin
Hi Alin_20.
You did not properly set your conditional formatting in your file, that is why it was not working.
I set the conditional formatting in the attached file correctly and it works now.
plz see attached.
- JamilApr 12, 2019Bronze Contributortry to see if you have already a CF set for that cell with a different formula. then it will work. so you will need to use a workaround. use countblank function instead.
- hiepkt12Apr 09, 2019Copper Contributor
I found this code to be used in my file but I want to switch from "Sub" to "Function". That is, I want the result of the yellow box displayed in the column count color yellow.
Help me!!
Thank you very much!!
Sub DisplayFormatCount()'Updateby20150305Dim Rng As RangeDim CountRange As RangeDim ColorRange As RangeDim xBackColor As LongDim xFontColor As LongOn Error Resume NextxTitleId = "KutoolsforExcel"Set CountRange = Application.SelectionSet CountRange = Application.InputBox("Count Range :", xTitleId, CountRange.Address, Type: = 8)Set ColorRange = Application.InputBox("Color Range(single cell):", xTitleId, Type: = 8)Set ColorRange = ColorRange.Range("A1")xReturn = 0For Each Rng In CountRangeqqq = Rng.Valuexxx = Rng.DisplayFormat.Interior.ColorIf Rng.DisplayFormat.Interior.Color = ColorRange.DisplayFormat.Interior.Color ThenxBackColor = xBackColor + 1End IfIf Rng.DisplayFormat.Font.Color = ColorRange.DisplayFormat.Font.Color ThenxFontColor = xFontColor + 1End IfNextMsgBox "BackColor is " & xBackColor & Chr(10) & "FontColor is " & xFontColorEnd Sub - JamilApr 09, 2019Bronze ContributorYou are most welcome.
Yes. you can simply add another condition that cell ="" then select "No color" in the fill - LauraferApr 01, 2019Copper Contributor
That worked perfectly! Thank you so very very much! One more question...Is there any way to have the cells that have no data in them remain uncolored? I was playing with the formulas but as I'm sure you've figured out by now I'm not the best at them. Everything else is working beautifully and I am grateful for what I've got but... :)
- JamilMar 29, 2019Bronze Contributor
thanks for your kind words.
I saw your workbook and there are many issues with the CF formulas you set.
Double condition set for a single cell with contradicts each other.
so example in the cell E28 there are two formulas in CF and both of them contradicts each other.
=IF(E28>D28,"true","pass")
=IF(E28<C28,"True","pass")another issue is that you set IF formulas to return test in CF which is not correct way to set formula.
for example if you want a condition to turn red if certain condition is met then you can simply use equal sign and there is no need for IF function, in fact IF function ruins it.
for example if you want to turn cell E28 to red if its value is greater than D28 then you can simply use =E28>D28 simply as this. CF will return True if condition is met and false if it is not met.
Please see attached your workbook.
I have added two more UDFs so that you can debug and find the problem.
The first UDF return the formula used in the CF for that cell
Function CondFormulaformula(myCell, Optional cond As Long = 1) As String
CondFormulaformula = myCell.FormatConditions(cond).Formula1
End Functionthis second formula evaluates the formula used in CF and returns the evaluated value.
Function CondFormula(myCell, Optional cond As Long = 1) As String
CondFormula = Application.Evaluate(myCell.FormatConditions(cond).Formula1)
End Functionyou can see in the column H which i highlighted in yellow. the evaluated CF from your formulas and they are not consistent. with the help of two addtional UDFs you can debug your CF formulas and get the correct result.
Hope this helps.
- LauraferMar 29, 2019Copper Contributor
Good morning Jamil! This thread has been SO helpful! I really appreciate it. Of course, since I'm joining I'm having trouble lol.
I added the UDF and made sure that the CF I'm using is based on formulas however I can't seem to get the results to be stable.
This is a fairly simple application and I'm feeling pretty dumb right now as I'm sure whatever it is I'm doing wrong is an easy fix.
Would you mind terribly taking a look? I tried a couple different formulas in the CF but even the straight-forward ones don't always end in the correct result. Sometimes the count is working and sometimes it isn't.
Any help you can give me would be much appreciated. This is making me nuts lol.
Thanks!
- JamilFeb 19, 2019Bronze ContributorIt should work. i suspect there must be an issue with the way CF range is set.
if you upload a dummy sample, i can take a look. - Ron_2019Feb 18, 2019Copper Contributor
When I use the formula, it works rows in the column except for the 2nd row of data. It does not count that row. Has anyone hit this issue or found a way around it? It correctly counts all of the other rows so I know it is looking at the condition formatting.