Forum Discussion
sum by color when colors are set by conditional formatting
- Jan 19, 2017
Hi matt nipper,
since you mentioned that you "have done an exhaustive search online" i have come up with a solution for you, although it has two limitations A) it will only work, if your rules of conditional formatting is created using conditional formatting rules with formula aka (use a formula to determine which cells to format) and B) the UDF will only work if sum range is more than one cell another word, it will not sum a single cell, as well as the conditional formatted range is more than one cell.
the example file, you can download it from here. I could not upload it here, as it is a Excel Macro-enabled Workbook that contains the 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
in the attached example file you can see that from A3:G16 cells are formatted using Conditional Formatting.
User Defined Function (UDF) is placed in cells J2 & J3 and cells I2 and I3 are the criteria color used as reference inside the UDF in J2 and J3.
I hope this helps you.
Edit: uploaded file and updated the code to the correct one.
Jamil Hi Jamil, i've tried the vba you provided to sum colored cells from conditional formatting. But it doesnt work. Could you take a look my worksheet to see where the code went wrong??
Thank You.
the conditional formatting applied range and the range use in the UDF are not the same. that is why it does not work.
- Hector_RMar 22, 2022Copper Contributor
Hi Jamil,
Good day, i've been trying to adapt my document to all the ones you already shared to count the number of cells colored by a conditional formatting but no succeed.
On my file i'd like to count the number of cells in red, yellow and green.
Could you help me with it?
if you allow me i could send it directly to you by message as i cant upload any document into this message.
- Ambertje01Jul 23, 2021Copper Contributor
Jamil , I've done everything as mentioned in this thread but I still get the #value message.
The cells I want to count are colored via Conditional Formatting.
The reference cell, I tried it with Conditional Formatting and without but with no luck.
Both times I get the #value error.
When I give my reference cell a different color with CF, then I still get: #value
When I give my reference cell a different color without CF, then I get: NO-COLOR.
Any clue where I go wrong?
In my attached example, the formula is in cel BD7.