• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

SUM by colour is not giving correct results

Hi

In the attached sheet sum by colour is not working or not giving proper results by using the below VBA Code

Example in cells BS6 and BR33 sum value should be 100 but there is 99

Likewise many cells different values are there.

Can anyone check and correct the code or guide me on what's the problem if the code is correct?

Thanks in advance.

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
 

Attachments

  • SUM by cell colours.xlsb
    21.2 KB · Views: 8
You may also affect Double type to your function.​
The other way is to search by format like I showed you in one of your previous theads …​
 
Thanks for your message

It is working in that sheet perfectly, But when I transfer to my original datasheet then it is not showing proper results as there is many different colours and structure of data.

Can you please suggest some other code or excel formula to get the results.
 
VBA on Excel Mac can be a mess so you must specify you are on Mac when creating a thread​
as many VBA solutions under Windows can't work under Mac …​
But when I transfer to my original datasheet then it is not showing proper results as there is many different colours and structure of data.
The better is an attachment relative to your issue with an accurate explanation where it fails, what is the expected result …​
For a column you can use a filter and an easy worksheet formula even under VBA.​
An issue may come from human eyes : some cells appear to be the same color but in fact not when checking the cell property …​
 
Just in case try this revamped version :​
Code:
Function SumByColor@(RColor As Range, Rg As Range)
         Dim Rc As Range
    For Each Rc In Rg
          If Rc.Interior.Color = RColor.Interior.Color Then SumByColor = SumByColor + Rc.Value2
    Next
End Function
You may Like it !​
 
Ok, so it was not a Mac concern but the common trap about the 'so so' accuracy under Excel / VBA​
revealing how important is the choice of data types of variables.​
As I yet demonstrated seven years ago how Excel / VBA evaluates an easy operation like 20.4 - 19.6 in this thread :​
According to this demonstration, for accountancy / financial numbers up to 4 decimals the accurate data type is Currency
the reason why my post #10 revamped version well works with your real workbook …​
 
Ok, so it was not a Mac concern but the common trap about the 'so so' accuracy under Excel / VBArevealing how important is the choice of data types of variables.
Yes, Thanks.

According to this demonstration, for accountancy / financial numbers up to 4 decimals the accurate data type is Currency
the reason why my post #10 revamped version well works with your real workbook …
Well Noted.
 
Back
Top