Sumif function doesn't work with ColorIndex criteria

whitehawk81

Board Regular
Joined
Sep 4, 2016
Messages
66
Hi,
I just found an issue concerning the Sumif function. When I enter the ColorIndex as criteria, it gives 0 as result. I tried to enter the formula in many ways, I even setup a ColorIndex function to check if the color index of the cell in criteria is actually the same as for the cells in criteria range. Basically I have a table, where only the cells in the first column have a specific background color. I wanted to sum the other columns based on the background color of the cells in column A.
Here are some of my attempts:

=SUMPRODUCT(--(ColorIndex(DS[DSname])=ColorIndex(A45));DS[MBB])

=SUMIF(DS[DSname];ColorIndex(A71);DS[MBB])

Code:
Function SumifByColor(critRange As Range, CellColor As Range, rRange As Range)Dim cSumif As Long
Dim ColIndex As Integer
ColIndex = CellColor.Interior.ColorIndex


For Each cl In rRange
  If critRange.Interior.ColorIndex = ColIndex Then
    cSumif = WorksheetFunction.SumIf(critRange, CellColor, rRange)
  End If
Next cl
SumifByColor = cSumif
End Function

Does anyone have an idea, how this can be done?
 

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
Were the cells colored manually or by Conditional Formatting? If the latter tells us the rule used in CF.

M.
 
Upvote 0
The cells were colored manually and when I use this ColorIndex function, I get the correct index numbers for the cells.
Code:
Public Function ColorIndex(CellColor As Range)ColorIndex = CellColor.Interior.ColorIndex
End Function
 
Upvote 0
Thanks! Actually I got the ColorIndex function from the first link :) These examples sum the values actually in the colored cells, but in my case the cell values, which I want to sum, are not in colored cells.
 
Upvote 0
But of course you know how to adapt the examples to add the neighboring cells. In fact, you have already done this in the function shown in post #1 :)

M.
 
Upvote 0
I just found a solution for this issue here:
https://stackoverflow.com/questions...la-based-on-background-color-of-adjacent-cell

I changed it a bit and now it works perfectly :)

Code:
Public Function ColorSum(CellColor As Range, myRange As Range, x As Integer) As Variant

Dim rngCell As Range
Dim total As Variant
Dim ColIndex As Integer
ColIndex = CellColor.Interior.ColorIndex


For Each rngCell In myRange.Cells


    If rngCell.Interior.ColorIndex = ColIndex Then
    total = total + rngCell.Offset(0, x).Value
    End If


Next rngCell


ColorSum = total


End Function
 
Upvote 0

Forum statistics

Threads
1,213,549
Messages
6,114,261
Members
448,558
Latest member
aivin

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top