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.
Hi Jamil. Thank you so much for this thread. I don't know if it's still active, but I'm hoping that you'll be able to help me.
I tried using your attached example file and added a third color, which doesn't work for me. I did conditional formatting using formulas, but with an AND statement. Can you have a look at my file and tell me if I made an error, or if the VBA code doesn't allow for "more advanced" formula statements?
BR Esben
The UDF did what it supposed to do. It did not work in the example you shared because when you set the third condition in red color=AND(A3>50,A3<60) this was already overlapping with the green condition, meaning that green was already counting anything above 50 . Therefore, you had a wrong count on the green when I changed the green to >=60 then it worked. see in the attached example.
- EsbenaamMay 07, 2020Copper Contributor
Hi Jamil
Thank you for the quick reply! Something seems to by off with my computer/excel.
When I first open the example file you sent, I am able to see the formula working as intended. However, after enabling editing, the formula stops working and gives me the "#VALUE!" error message - as shown in the second picture. Do you have an explanation for this? Could it be that I'm using excel in a different language? (Danish)
BR Esben
- JamilMay 07, 2020Bronze Contributor
Hej, It has nothing to do excel language being in Danish.
Can you try the attached version? if you still get Value error. try to press F9 and see if what happens.
if in all cases, it returns Value Error. see if you have any macros in PERSONAL.XLSB excel start up folder.
also, some add-ins does not let the UDF to work properly. so if you have any add-in activated, try to deactivate it temporary and check the file.
- EsbenaamMay 11, 2020Copper Contributor
Hi Jamil
I tried opening the excel file on my personal laptop and it worked fine, however it still doesn't work on my work laptop. On my work laptop I do not have any add-inns activated and my XLSTART folder is empty. Pressing F9 just makes the VALUE! flash and nothing happens.
My personal computer uses Microsoft 365 for enterprise (excel version 2004), and my work computer uses Microsoft Office 365 ProPlus (excel version 1908) - could this have something to do with the error?
BR Esben