Excel Office 365 - SumByColor and Rounding

EMGVT

New Member
Joined
Sep 28, 2023
Messages
1
Office Version
  1. 365
Platform
  1. MacOS
I know there are prior posts to this, but I am having issues with Excel rounding up when trying to do sum by color. This is the code that was originally suggested that I use:

Function SumColor(SumRange As Range, ColorCode As Range)
Dim ColorCodeValue As Integer
Dim TotalSum As Long
ColorCodeValue = ColorCode.Interior.ColorIndex
Set rCell = SumRange
For Each rCell In SumRange
If rCell.Interior.ColorIndex = ColorCodeValue Then
TotalSum = TotalSum + rCell.Value
End If
Next rCell
SumColor = TotalSum
End Function


I have very basic knowledge of VBA, so any help would be highly appreciated!
 

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
The reason that the value returned by SumColor is sometimes rounded up is that you've declared TotalSum as Long. As such, the variable can't store decimal numbers. Therefore, declare it as Double. I would also suggest that you declared the return type of the SumColor function as Double as well. Also, I would suggest that you use the Color property of the Interior object, instead of the ColorIndex property so that you can refer to the complete palette of colors. This would mean that you would have to declare ColorCodeValue as Long. Try the following instead...

VBA Code:
Function SumColor(SumRange As Range, ColorCode As Range) As Double

    Dim ColorCodeValue As Long
    Dim TotalSum As Double
    Dim rCell As Range
   
    ColorCodeValue = ColorCode.Interior.Color
   
    For Each rCell In SumRange
        If rCell.Interior.Color = ColorCodeValue Then
            TotalSum = TotalSum + rCell.Value
        End If
    Next rCell
   
    SumColor = TotalSum
   
End Function

Hope this helps!
 
Upvote 0

Forum statistics

Threads
1,223,952
Messages
6,175,595
Members
452,656
Latest member
earth

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