Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2012-03-15
7
Medium Priority
?
430 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Industry Leaders: 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

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

Question has a verified solution.

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

This tutorial explains how to create a series of drop-down lists that are dependent upon prior selections to guide (“force”) the user to make the correct selection and reduce data errors within Microsoft Excel. Excel 2010 was used for this tutorial;…
Do you use a spreadsheet like Microsoft's Excel?  Have you ever wanted to link out to a non excel file on your computer or network drive?  This is the way I found to do it!
The viewer will learn how to use the =DISCRINV command to create a discrete random variable, use this command to model a set of probabilities and outcomes in a Monte Carlo simulation, and learn how to find the standard deviation of a set of probabil…
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …

636 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