?
Solved

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

Posted on 2012-03-15
7
Medium Priority
?
446 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:Frank White
Frank White earned 200 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 12

Accepted Solution

by:
kgerb earned 800 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:Frank White
Frank White earned 200 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

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

Question has a verified solution.

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

This article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
After seeing numerous questions for Dynamic Data Validation I notice that most have used Visual Basic to solve the problem. This suggestion is purely formula based and can be used in multiple rows.
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.

750 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