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 Julie,
please see attached workbook sample that shows how you can use the UDF.
please post back, if you have any question.
Hi Jamil,
Not sure why it is not working on my computer, because after pressing "edit" on Your document, it also got error value "#Name?", and You can see on my document Attached how it looks with error #VALUE! I wasn't able to attach the Excel file With Makro enabled, but have of course had the makro enabled file so far.
Great if you can advice.
- JamilMar 19, 2018Bronze Contributor
Hi Julie,
I checked your file.
it was not working because A) you need to enable macros in order to be able to use the UDF.
B) the sample file you upload, you had only cells for criteria colored, but there wasn't any conditional formatting set in your workbook, that's why It did not work.
I have recorded a video that shows how you can set up your conditional formatting.
please download the attached video and watch it. also i have attached your workbook with conditional formatting set in it.
- ThanathoziaNov 23, 2020Copper Contributor
Hello Jamil !
I've tried to read your answers and I have managed to install the UDF for counting condition colored cells, but at the most I am only managing the formula to say "NO-COLOR" (sum is not relevant but I've installed it too). I have looked in your example files and the video tutorial you made, but I am still not managing to get it to work, macros should be enabled. I need the sheet to count the amount of red/green cells both horizontally and vertically. The cells are formatted so that when a number within range is added to the cell it turns green, but if a number out of range is added or if the cell is empty it goes red - that part is already done (ranges are written above the cells in blue). Now I just need the color counting in order to calculate what passed and what failed. I have attached a copy of my excel document, and I would be really grateful if you had the time to look through it and check what's missing.
Kind regards, Madeleine Larsson
(English is not my native language, so I apologize if any words are improperly used)
- JamilNov 24, 2020Bronze Contributor
I checked your file. the conditional formatting you set is not using a formula that is why it is not working.
- mecerratoJan 04, 2019Copper Contributor
Hi Jamil I was able to successfully use your code to count my conditionally formatted cells but my data changes regularly and the function doesn't automatically refresh. In order for it to pick up the changes i have to refresh the cell with F2 and then enter, any solution for that?
- JamilJan 04, 2019Bronze Contributormecerrato
first try to check that your workbook calculation setting is "Automatic"
if it is automatic and still did not work then try putting the below line after the first line of UDF.
Application.Volatile
- 7_HeavenDec 14, 2018Copper Contributor
Hi Jamil
I stumbled upon your code as I am using conditional formatting formula to highlights my cell. Now I want to count the highlighted cells.
Similar to Julie, the VBA returns "no color" instead of the numbers. I have done everything that I can possibly can.
I suspect it doesn't work because I use this formula in my conditional formatting =if(isblank($K$1),0,(search($K$1,A5))). A5 is where my data starts. K1 is where when someone type a text and that text is in the data, it will highlight those texts. Attached is an example (which didn't quite work but you'll get the idea).
Thanks for your help.
- JamilDec 19, 2018Bronze Contributor
hi 7 Heven,
You have used Search function and also whole column reference A:A which is not compatible with the UDF. I have modified the formula and range in the attached example and the UDF works.