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...

ABC
1=SumByColor(A3;B1:B3) = 3
should have been 2,7
RED / 1,5BLUE / 2,5
2=SumByColor(A4;C1:C3) = 4
should have been 5
RED / 1,2BLUE / 2,5
3REDBLUE / 2,2RED / 1,2
4BLUE

<tbody>
</tbody>


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

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
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,216,120
Messages
6,128,948
Members
449,480
Latest member
yesitisasport

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