Solved

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

Posted on 2012-03-15
7
409 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 12

Accepted Solution

by:
kgerb earned 200 total points
Comment Utility
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
Comment Utility
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
Comment Utility
www.asap-utilities.com has this functionality.

Regards,
Zack Barresse
0
 

Author Closing Comment

by:BBlu
Comment Utility
Thanks, Guys.  Just what I was looking for.
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

Introduction While answering a recent question (http:/Q_27311462.html), I created an alternative function to the Excel Concatenate() function that you might find useful.  I tested several solutions and share the results in this article as well as t…
Convert between Excel file formats (.XLS, .XLSX, .XLSM) with/without macro option David Miller (dlmille) Intro Over this past Fall, I've had the opportunity to see several similar requests and have developed a couple related solutions associate…
This Micro Tutorial demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.

771 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now