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
If you read through my earlier posts. I have clearly mentioned that UDF only works if the conditional formating is set using Formula. you have used formulas and non-formula options to set your conditional formatting. that is the first issue that is causing the UDF not to work.
the second issue is that your are using the whole column references in your CF which is not going to work.
third issue is that your CF is set by column and you are trying to do the count in UDF using row. If you look at the example I illustrated in answer to Alin. you can see that i modified the CR using painbrush to reflect the rows.
so you need to fix the three of the issues i pointed, so that the UDF would work.
Hi Jamil, thanks for your quick response.
I am trying to count the cells in column P in each different section and total these to a specific cell- these have been condition formatted to change colour depending on the values that are put into them, which are summed by the Moulds & Yeasts Columns (Q&R)- for example, I want the total of green cells within P9-18 to be totalled inside C51 and then the orange and red cells from P9-18 in C52 and C53, but I want C51, 52, and 53 to be able to update when changes occur in P9-18 and the colours change. I have managed to add a VBA code where the totals can recognise the colours but I cannot get them to update when the colours change from their value.
I have attached a copy of the workbook without the macro enabled as it was the only way the site would allow me to send it. hope this helps.
Many thanks for your help!
Kind regards,
Chris
- JamilMay 15, 2020Bronze Contributor
Hi Chris,
What you are after is possible and this UDF can do that.
If you read my earlier messages in this thread, you will notice that I mentioned that for this UDF to work, the conditional formatting rule should be creating using the option "use a formula to determine which cell to format"
to demonstrate, I have changed the rules of conditional formatting for range P9-18 and replaced it with formula. I replaced the rule, between 0 to 9 with =AND(ISNUMBER(P9),LEN(P9)=1) and also the other two colors as you can see it in the attached file.
then I placed the UDF as an example in the cell C51 which correctly returns the value. given this example, you can replicate this for other ranges/cells.
plz see attached file.