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 I have the same issue as Hyosun. I've entered the VBA code but cannot get it to work. No matter what I do it keeps showing the #Value. I'm trying to count duplicate text based values which seems to be causing some problems. Can you point me towards where the error might be occurring and or a resolution.
Thanks
Noel
Hi Noel,
The VALUE error because your conditional formatting was not set using formula.
If you read my earlier post, I have highlighted that UDF only works if the conditional formatting is set by using formula and not the built-in features of CF.
So, in the attached workbook, I have set the Conditional Formatting using formula and then the UDF works.
please note that SUBTOTALs are not counted as duplicate. please see attached file and examine it by yourself.
Hope it helps.
- Jo_NolajFeb 21, 2019Copper ContributorThank you very much!
- JamilFeb 13, 2019Bronze Contributor
Hi Jo_Nolaj,
I looked at your workbook, the conditional formatting range and the range used in the UDF was not consistent. i changed it now and it works. plz see attached. please note that instead of 4 CF you could set them with OR function all in one. plz see the CF which i set. the only thing is that CF is replicated for each row using format painter.
- Jo_NolajJan 30, 2019Copper Contributor
Hi, Sir Jamil! Could you kindly help me how to make the UDF and formula you shared on counting the number of rows colored thru conditional formatting work? I copied and paste the UDF for this and followed the formula in your attached sample file, but I cannot get it right. The total is always zero. Here's the file I am working. Thank you very much!
- David Mejia-ZaccaroSep 10, 2018Copper Contributor
This is awesome, thank you so much for your help!
- JamilSep 07, 2018Bronze Contributor
Dear David,
The issue with your file was, that UDF was using single row range as the range input, while the Conditional Formatting was applies from L2 to Z8 . So, i modified the file Conditional Formatting and now it works.
Also inside the CF formula, you used two functions, while this was not cause of error, but to make it simple and faster, i simplified it with a single function of "Columns".
so =COLUMN(L2)-COLUMN($L2)+1>=$AE2 became =COLUMNS($L2:L2)>=$AE2
Please download and see attached workbook.
- David Mejia-ZaccaroSep 06, 2018Copper Contributor
Dear Jamil,
Your help has been really enlightening. If you are available, could you please help me by taking a look at his file?
I am using the VB code you created, but the formula doesn't seem to recognize the CF color and I keep getting the #VALUE! refference.
Could you let me know how to fix this?
- Noel JacobsAug 30, 2018Copper Contributor
Thank you Jamil- so easy when you know how. Much appreciated.