Solved

VBA Function (UDF) to sum cells that match a color

Posted on 2012-03-15
7
418 Views
Last Modified: 2012-06-27
I'm trying to write a function that will take two arguments (range,cell).  I'd like to sum all of the cells inside the 'range' that match the color inside of 'cell'.  See attached for an idea of what I'm talking about.  Does anyone have a function that does this?
SumByColor.xlsx
0
Comment
Question by:BBlu
7 Comments
 
LVL 3

Assisted Solution

by:DaFranker
DaFranker earned 50 total points
ID: 37726439
You can use the .Interior.ColorIndex property for this. Something along the lines of:

Function ColorSum(TheRange as Range) As Long
For Each c in TheRange
    If c.Interior.ColorIndex = 6 Then mySum = mySum + c.Value
Next
ColorSum = mySum
End Function

Open in new window


(add definitions and whatever other code is needed, as appropriate and depending on whether you use Option Explicit, which I recommend)

The "= 6" for ColorIndex is the index from the current color palette. I'm sure if you look around, you can find which numbers you'll need for your particular purpose.

EDIT: This link might be helpful in figuring out how to use the ColorIndex property with the above method.
0
 
LVL 12

Expert Comment

by:kgerb
ID: 37726467
Here is one where the color index is not static but dependent on the color index of the cell specified in the 2nd argument.
Function SumByColor(rng As Range, cll As Range) As Double
Dim r As Range
For Each r In rng
    If r.Interior.ColorIndex = cll.Interior.ColorIndex Then SumByColor = SumByColor + r
Next r
End Function

Open in new window

Kyle
0
 
LVL 10

Expert Comment

by:Anthony Berenguel
ID: 37726493
try this.. although it looks pretty similar to the solution above

Function sumByBGC(r As RANGE, c As String)
    'sum by background color
    Dim result As Long
    result = 0

    Debug.Print c
    For Each cell In r
        Debug.Print cell
        With cell
                If cell.Interior.ColorIndex = RANGE(c).Interior.ColorIndex Then
                result = result + cell.Value
            End If
        End With
    Next
    sumByBGC = result
End Function

Open in new window

0
Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

 
LVL 12

Accepted Solution

by:
kgerb earned 200 total points
ID: 37726496
Here's a new one that updates with the worksheet recalc.
Function SumByColor(rng As Range, cll As Range) As Double
Dim r As Range
Application.Volatile
For Each r In rng
    If r.Interior.ColorIndex = cll.Interior.ColorIndex Then SumByColor = SumByColor + r
Next r
End Function

Open in new window

Kyle
0
 
LVL 3

Assisted Solution

by:DaFranker
DaFranker earned 50 total points
ID: 37726685
For a (small) speed boost  with Kyle's second function example, add a new variable and set it to cll.Interior.ColorIndex. This will shave off a few VBA-to-Worksheet reads, which can make a difference in performance if you're using this UDF in volatile mode across a lot of cells. Then compare each r against this new variable.
0
 
LVL 14

Expert Comment

by:Zack Barresse
ID: 37726785
www.asap-utilities.com has this functionality.

Regards,
Zack Barresse
0
 

Author Closing Comment

by:BBlu
ID: 37727058
Thanks, Guys.  Just what I was looking for.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Improved? Move/Copy Add-in Replacement - How to avoid the annoying, “A formula or sheet you want to move or copy contains the name XXX, which already exists on the destination worksheet.” David Miller (dlmille)  It was one of those days… I wa…
This article descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question