Adjust SumByColor to allow to sum by another variable

arun96

New Member
Joined
Mar 15, 2021
Messages
2
Office Version
  1. 365
Platform
  1. Windows
  2. MacOS
I currently have this, but don't know how to add it to sum by both colour and another column. Assuming the numbers are all coloured by their status, I need to sum based on that as well as the category in column A - how do I adjust the VBA code below to allow for that?

CategoryMonth AMonth B
A100100
B2000200
A100300
B200400
B100500
B300600

Function SumByColor(CellColor As Range, rRange As Range)
Dim cSum As Long
Dim ColIndex As Integer
ColIndex = CellColor.Interior.ColorIndex
For Each cl In rRange
If cl.Interior.ColorIndex = ColIndex Then cSum = WorksheetFunction.Sum(cl, cSum)
End If
Next cl
SumByColor = cSum
End Function
 

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
VBA Code:
Function SumByColorAndCat(CellColor As Range, Cat As Variant, rRange As Range)
    Dim cSum As Long
    Dim ColIndex As Integer
    ColIndex = CellColor.Interior.ColorIndex
    For Each cl In rRange
        If cl.Interior.ColorIndex = ColIndex And Range("A" & cl.Row) = Cat Then cSum = WorksheetFunction.Sum(cl, cSum)
    Next cl
    SumByColorAndCat = cSum
End Function
 
Upvote 0
VBA Code:
Function SumByColorAndCat(CellColor As Range, Cat As Variant, rRange As Range)
    Dim cSum As Long
    Dim ColIndex As Integer
    ColIndex = CellColor.Interior.ColorIndex
    For Each cl In rRange
        If cl.Interior.ColorIndex = ColIndex And Range("A" & cl.Row) = Cat Then cSum = WorksheetFunction.Sum(cl, cSum)
    Next cl
    SumByColorAndCat = cSum
End Function
Hello! This is so helpful but I don't think it's quite right, I need to essentially sum column C based on the cell colour in column C but the category of the rows that is defined in Column A.

Thank you!
 
Upvote 0
I think the function does that.
sum cells with color and criteria.xlsm
ABCDEFG
1CatMonth1Month2Month3Cat:b
2a414Color:
3b424sum:6
4a434
5b444
6a454
7b464
8e474
9d484
10a494
Blad1
Cell Formulas
RangeFormula
G3G3=SumByColorAndCat(G2,G1,C2:C10)
 
Upvote 0

Forum statistics

Threads
1,223,164
Messages
6,170,444
Members
452,326
Latest member
johnshaji

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