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,
Feel bad for bothering you with what I think is going to be a simple fix, but for the life of me I can't figure what I'm doing wrong here.
In the attached, which I've downloaded from one of your posts, I have a series of conditionally formatted cells (for instance AJ9:AU9) which when TRUE one or more cells will turn purple.
I'm trying to use your formula in cell AX9, and the minute I try to change the RANGE to AJ9:AU9, checking against color purple in cell AW9, I get "#VALUE".
If you don't mind, why does it error out?
I appreciate it, and hope you have a good weekend. Stay safe. 😉
I will take a look later today. It is too early in the morning here.
- JamilMar 18, 2021Bronze Contributor
Hi GSMCONNECT
you misunderstood on the use of the SUMPRODUCT formula. it was not meant to be used inside conditional formatting. it was meant to replace the UDF. I have now placed three formulas in cells B51, C51, and D51.
the formulas work just fine, so you do not need to use UDF SumConditionColorCellsplz see attached file with the formulas.
- GSMCONNECTMar 18, 2021Copper Contributor
Hi Jamil,
thank you very much for your reply
I started excel in safe mode and opened the file but still get a # VALUE error.
I also pasted the sumproduct formula you posted but still get a # VALUE error.
then I opened the file in excel 2007 which was installed in English but still get a # VALUE error.
the fact is I need a double condition in the CF of my excelfile.
can you please help?
thx
- JamilMar 17, 2021Bronze ContributorHI GSMCONNECT, Yes, it could be the language issue, but I am not sure.
in B51 you can use a formula and completely disregard the UDF. a formula to get that result is =SUMPRODUCT(($P$9:$P$18)*(ISNUMBER($P$9:$P$18))*(LEN($P$9:$P$18)=1))
and in Dutch =SOMPRODUCT(($P$9:$P$18)*(ISGETAL($P$9:$P$18))*(LENGTE($P$9:$P$18)=1)) - GSMCONNECTMar 17, 2021Copper ContributorHi Jamil,
thank you for your reply.
I sent the file to a friend of mine and he gets the # VALUE error also.
could this be a language problem? I'm from Belgium and our excel is in dutch, not in English.
thx - JamilMar 17, 2021Bronze ContributorHi GSMCONNECT, Mould Template1.xlsm is not my file, if you downloaded from one of the answers in the thread, it could be my reply to one of the questions in the thread. so, I did not ever create such file. However, I quickly looked at the file you shared. I do not get any #VALUE error. Perhaps, it is some Add-In or any other file in your excel start up folder that interferes with the UDF. Try to disable them and then the error may disappear.
- GSMCONNECTMar 17, 2021Copper Contributor
Hi Jamil,
I downloaded "Mould Template1.xlsm" on one of your posts. For a reason I don't understand I get a "# VALUE" error in B51it has something to do with the AND function in the CF.
when I don't use the AND function in CF it works.
the fact is I need the AND function in the CF of my sheet
Can you please help?
thx