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.
request for your help in the following problem.
I need the help to write the VBA code or standard function to do the SUMIFS - with both criteria as colour and value.
Urgent - response requested.
Shoaib
Hi Shoaib,
though your question is not related to this thread, I will try to answer it.
In your example, you can make your SUMIFS work by using a helper column and also a UDF which is embedded in the attached example file.
So the helper column return the color index number of cell background as you can see in the attached example and then I used the UDF in the helper column to return the color index value and then used that range as the criteria range for the SUMIFS function. Please note that the example is only set to work for the background cell color set by user and not via Conditional formatting.
Besides, there is also another way to get the same result without using SUMIFS.
simply use auto filter and filter by the desired color and then by your desired value and then use the built in Subtotal function to sum only filtered cells like this =SUBTOTAL(109, yoursumrange)
- Muhammad shoaib asad KhalidNov 11, 2018Copper Contributor
Dear Jamil,
thanks a lot for your favor and response, it works ok.
- JamilNov 12, 2018Bronze ContributorDear Shoaib,
You are welcome. Thanks for your feedback.
- Muhammad shoaib asad KhalidNov 11, 2018Copper Contributor
Dear Jamil,
thanks a lot for your favor and response, it works ok. I also have received another solution (Bosco);
1] Define 2 Names :
Select B17 >> Define Name >>
Name (1) : SumRgn
Refer to :
=INDEX($D$5:$I$10,MATCH($A17,$A$5:$A$10,0)+MATCH(LOOKUP("zz",$B$15:B$15),{"Onsite";"OffSite"},0)-1,0)
OK
Name (2) : SumColor
Refer to :
=SUMPRODUCT(0+(GET.CELL(63,IF(1,+OFFSET(SumRgn,,COLUMN($D$4:$I$4)-MIN(COLUMN($D$4:$I$4)))))=GET.CELL(63,B$16)),SumRgn)OK
2] In B17, formula copied across to G17 and all copied down :
=SumColor
p.s. : Get.Cell() is a Excel 4 Macro function, so you need to save file as Macro-Enable Workbook xlsm type. - DG EwingNov 09, 2018Copper Contributor
Hi Jamil,
I sent you a msg, before discovering this thread is still active. So I am attaching the file,
My conditional formatting evaluates row 18 and colors cells A3:G16 if the corresponding A18:G18 value doesnt match....
but as you can see something is really hosed up.
- DG EwingNov 09, 2018Copper Contributor
Jamil,
This is my real data (extract--no names and a few rows/cols)....What I really want to do is the conditional formatting as is done correctly (red values where the evaluated value doesn't match row 7) and then count the red values per row (col a)
I tried in a xlsm and xls...neither has worked.
thank you so much,
dg
- JamilNov 09, 2018Bronze ContributorHi dg
I am not home now. Once I reach home, I will take a look at your excel file.