=Sum in for Color cells in Column K

zone709

Well-known Member
Joined
Mar 1, 2016
Messages
2,129
Office Version
  1. 365
Platform
  1. Windows
I highlight down a sheet in Column K what i need to pay. Since the sheet is so long. It it possible to Sum the cells only highlighted in yellow thank you.


Rich (BB code):
  .Pattern = xlSolid
        .PatternColorIndex = xlAutomatic
        .Color = 65535
        .TintAndShade = 0
        .PatternTintAndShade = 0
 

Excel Facts

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.
You can't do this with native functions, since none of the standard functions can determine the formatting of a cell. But you can do it with a UDF (User Defined Function). Open a COPY of your workbook. Right click the sheet tab on the bottom and select View Code. Press Alt-IM to Insert a Module. Paste the following code in the window that opens:

VBA Code:
Public Function ColorSum(R As Range, cr As Range)
Dim cel As Variant, CFColor As Long, s As Double

    Application.Volatile

    s = 0
    On Error Resume Next
    For Each cel In R
        CFColor = Evaluate("Helper(" & cel.Address() & ")")
        If CFColor = cr.Interior.Color Then s = s + cel.Value
    Next cel
    
    ColorSum = s

End Function

Private Function Helper(ByVal R As Range) As Double
    Helper = R.DisplayFormat.Interior.Color
End Function

Go back to Excel and use the function like this:

Book1
ABCD
1CodeSumAmount
2Rent171
3Food132
4Gas183
54
65
76
87
98
10oops
1110
1211
13
Sheet1
Cell Formulas
RangeFormula
B2:B4B2=colorsum($D$2:$D$20,A2)


The first parameter of the function is the range to search, the second is a cell with the color you want to sum. Also note that Excel doesn't force a recalculation when a cell is formatted. So if you format a cell red, the total won't change until you press F9 or update a cell.
 
Upvote 0
thanks for the reply. I haven't gotten to it yet but i will let you know.
 
Upvote 0

Forum statistics

Threads
1,224,809
Messages
6,181,076
Members
453,020
Latest member
mattg2448

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