Counting by colours

lewislusby

New Member
Joined
Jul 11, 2011
Messages
9
Hi,
I've used a macro to sum cells of a similar colour and then used "=SumColor(cell,range)" and that works well BUT now I want to sum the cells in the column next to where the coloured cells are. Do I need to include a vlookup to the formula? Stuck!
 

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
Welcome to the board lewislusby,

The following is a modified version of a SumColor Function that takes a third argument which represents the column offset to sum. Called like this
'=SumColor(cell,range,offset)' If the offset 0 it will work just like SumColor. When the offset is a negative or positive number it will sum the cells that number left or right. If the offset is not zero the range may only be one column wide.

Code:
Function SumColorOffset( _
            matchCell As Range, _
            checkRng As Range, _
            colOffset As Integer)

    Dim cell As Range
    Dim matchColor As Integer
    Dim tempSum

    '// Offset Bounds Checking
    If checkRng.Column + colOffset < 1 Or _
            checkRng.Column + colOffset > Columns.Count Or _
            (checkRng.Columns.Count <> 1 And colOffset <> 0) Then
        SumColorOffset = CVErr(xlValue)
        GoTo errorExit
    End If

    matchColor = matchCell.Interior.ColorIndex
    
    For Each cell In checkRng
        If cell.Interior.ColorIndex = matchColor Then
            tempSum = WorksheetFunction.Sum(cell.Offset(0, colOffset)) + tempSum
        End If
    Next cell
    
    SumColorOffset = tempSum
errorExit:

End Function
 
Upvote 0

Forum statistics

Threads
1,224,507
Messages
6,179,181
Members
452,893
Latest member
denay

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top