SumByColor - without helper cells and with two decimals precision?

ChristianEllehammer

New Member
Joined
Aug 18, 2015
Messages
6
I'm using the vba code below to sum cells by their color and it's not really working to my need

1. It rounds the sum to nearest whole number. I need two decimals precision
2. It does some weird stuff, e.g. it sums 2 * 1,5 to 4 but it also sums 2 * 2,5 to 4...

[TABLE="class: grid, width: 500"]
<tbody>[TR]
[TD][/TD]
[TD]A[/TD]
[TD]B[/TD]
[TD]C[/TD]
[/TR]
[TR]
[TD]1[/TD]
[TD]=SumByColor(A3;B1:B3) = 3
should have been 2,7[/TD]
[TD]RED / 1,5[/TD]
[TD]BLUE / 2,5[/TD]
[/TR]
[TR]
[TD]2[/TD]
[TD]=SumByColor(A4;C1:C3) = 4
should have been 5[/TD]
[TD]RED / 1,2[/TD]
[TD]BLUE / 2,5[/TD]
[/TR]
[TR]
[TD]3[/TD]
[TD]RED[/TD]
[TD]BLUE / 2,2[/TD]
[TD]RED / 1,2[/TD]
[/TR]
[TR]
[TD]4[/TD]
[TD]BLUE[/TD]
[TD][/TD]
[TD][/TD]
[/TR]
</tbody>[/TABLE]


I'm using Excel 2016 on a Mac and need this this functionality of summing cells based on their colors, without the use of helper cells and with two decimal precision.

Thank you :)


Code:
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
 
Last edited:

Excel Facts

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)
Try changing
Code:
cSum As Long
to
Code:
cSum As Double

Although it isn't causing your issue you also haven't declared cl as a variable.
 
Last edited:
Upvote 0
You're welcome but also see my comment about cl in my edit to my original post.

Code:
Function SumByColor(CellColor As Range, rRange As Range)
    Dim cSum As Double, cl As Range, ColIndex As Long

    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
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,893
Messages
6,175,248
Members
452,623
Latest member
cliftonhandyman

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