How to sum up highlighted cells

Stevekent

Board Regular
Joined
Jul 24, 2013
Messages
109
Office Version
  1. 365
Platform
  1. Windows
I've a column say A1 to A20 with numbers. A2, A4, A10, A15 and A18 are highlighted with color. Now my question is, what formula can sum up these highlighted cells? Thanks in advance
 

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.
Is this manual highlight or conditional formatting?
If it is conditional formatting then you can create a formula using the same logic as the cf rule.
If it is manual highlighting then there is no native formula that will do it, you would need to use vba, either as a procedure or a udf.
 
Upvote 0
Maybe something like:
This code will overwrite A21 cell with the sum result.
VBA Code:
Sub test()
Dim x As Integer, mysum As Double
For x = 1 To 20
    If Cells(x, 1).Interior.Color <> 16777215 Then mysum = mysum + Cells(x, 1)
Next x
Range("A21") = mysum
End Sub
 
Upvote 0
If the cells are highlighted by conditional formatting and you want to use UDF to calculate
you need to create specific function.
This one can be used to sum cells formatted by rule "Formate only cells that contain non blanks".
Even if you have more than one condition formating in the function's range that overlaping.
VBA Code:
Function SumHighlightedCells(varRange2 As Range)

    Dim varRange1 As Range
    Dim varConCount As Integer, varNloops As Integer
    Dim varCondition As Boolean
    Dim varSum As Double
    
    For Each varRange1 In varRange2
        varConCount = varRange1.FormatConditions.Count
        For varNloops = 1 To varConCount
            If varRange1.FormatConditions(varNloops).Type = 13 Then
                varCondition = Application.Evaluate _
                (varRange1.FormatConditions(varNloops).Formula1)
                If varCondition = True And IsNumeric(varRange1) Then _
                    varSum = varSum + varRange1.Value / varConCount
            End If
        Next varNloops
    Next varRange1
    SumHighlightedCells = varSum

End Function

SumConditionsValues.PNG
 
Upvote 0

Forum statistics

Threads
1,213,552
Messages
6,114,278
Members
448,560
Latest member
Torchwood72

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