VBA code – Sumbycellscolor

You can try this program code. You get results of sum by color in "L1" . Color choices are in Range(M1:M3). You have to change row number number indication in the code as indicated by me. Image given after code shows the sample data. HTH

Sub SumByColorV()

    Dim ws As Worksheet
    Dim lastRow As Long
    Dim result As Long
    Dim cSum As Long
    Dim ColIndex As Integer
    Dim CellColor As Range
    Dim rRange As Range
    Set CellColor = ActiveSheet.Cells(1, 13) 'Vary row no 1 or 2 or 3 of column 13 (M) as per yr requirement
    lastRow = ActiveSheet.Cells(Rows.Count, "K").End(xlUp).Row
    Set rRange = ActiveSheet.Range(Cells(1, 11), Cells(lastRow, 11))
    ColIndex = CellColor.Interior.ColorIndex

    For Each cl In rRange
        If cl.Interior.ColorIndex = ColIndex Then
        cSum = WorksheetFunction.Sum(cl, cSum)
        End If
    Next cl
    result = cSum
    ActiveSheet.Cells(1, 12).Value = result
End Sub

Sample data with result

Leave a Comment