Sum by color background and if another condition

Sonic47

New Member
Joined
May 20, 2020
Messages
5
Hello,

I am trying to write/find a correct vba code able to sum the amount in a colored cell based on two conditions : the color of the background and based on another condition (here the fruit)
You can find a easy table showing the situation :
I want the formula to sum the amount in the cell based on the color yellow and if the fruit if an apple for exemple.
We can see the result below, but made manually.

I found a lot of code able to sum (without second condition) or code able to see the color code of the background, but I am not able to use "SUMIF" with it able to choose based on the fruit directly.

Could you please help me ?


1589981370096.png


Thank you very much.
 

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college
Can you post your table using the boards XL2BB app please as we can't copy/paste your image into Excel cells (the XL2BB icon in the reply window is a link to the instructions and the download for XL2BB).
 
Upvote 0
Proje Maliyet Öngürü.xlsm
ABCDEFGHIJKL
1JanuaryFebruaryYellowBlue
2apple16JanuaryFebruaryJanuaryFebruary
3banana2525Apple19Apple50
4mango802Banana00Banana3028
5apple53Mango802Mango00
6banana53
Sheet1
Cell Formulas
RangeFormula
G3,G5:H5G3=B2
H3H3=C2+C5
K3K3=B5
K4:L4K4=B3+B6
 
Upvote 0
You could use this UDF in combination with SUMPRODUCT
=SUMPRODUCT(--(A2:A6="apple)*(ColorOf(B2:C6=65535)*(B2:C6))

Like all color reading functions, it does not calculate when the user changes a cell's color, only when the user enters a value in any cell.
VBA Code:
Function ColorOf(aRange As Range) As Variant
    Dim Result As Variant
    Dim i As Long, j As Long
    Application.Volatile
    Result = aRange.Value
    For i = 1 To UBound(Result, 1)
        For j = 1 To UBound(Result, 2)
            Result(i, j) = aRange.Cells(i, j).Interior.Color
        Next j
    Next i
    ColorOf = Result
End Function
 
Upvote 0
Hello, thank you very much Mikerickson, but unfortunaterly in the document I must prepare, my colleague will change the colors of the cells.
I tried your code but I get an error for that .. I am trying to understand why.
 
Upvote 0
Your colleague will change the colors and the cells will reflect faulty results until they enter something in some cell.
And there is no way around that. VBA solutions don't work because changing a cells color do not trigger a Change event. UDF base solutions don't work, because changing a cell's color does not trigger calculation. The only way that your colleague will be sure of the results of any approach will be for him to force calculation (or call a macro if you go that direction).

The SUMPRODUCT formula that I posted was missing a quoteation mark. Ishould be put in G3. It should also be altered to
=SUMPRODUCT(--($A$2:$A$6=$F3)*(ColorOf(B$2:B$6=65535)*(B$2:B$6))
 
Upvote 0
Appreciate I am resurrecting a 3yo post... but I need this exact function.... However I'm getting #VALUE error when replicating the above setup exactly.... can anyone help?
Any assistance appreciated. Have uploaded my mainsheet using Xl2bb

apple.xlsm
ABCDEFGH
1JanuaryFebruaryYellow
2apple16JanuaryFebruary
3banana2525apple#VALUE!
4mango802banana#VALUE!
5apple53mango
6banana53
7
8
9
Sheet1
Cell Formulas
RangeFormula
G3G3=SUMPRODUCT(--(A2:A6="apple")*(ColorOf(B2:B6=65535)*(B2:B6)))
G4G4=SUMPRODUCT(--($A$2:$A$6=$F3)*(ColorOf(B$2:B$6=65535)*(B$2:B$6)))
 

Attachments

  • Screenshot 2023-05-17 at 15.55.13.png
    Screenshot 2023-05-17 at 15.55.13.png
    32.3 KB · Views: 9
Upvote 0
Try it like
Excel Formula:
=SUMPRODUCT(--($A$2:$A$6=$F2)*(ColorOf(B$2:B$6)=65535)*(B$2:B$6))
 
Upvote 1

Forum statistics

Threads
1,224,818
Messages
6,181,152
Members
453,021
Latest member
Justyna P

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