Sum by color using VBA in Microsoft Excel

Sum by color using VBA in Microsoft Excel

In this article we learn to how Microsoft Excel Sheet can calculate the sum of Cell with specific Color in defined range of Cell. we create the custom module by using VBA in Microsoft Excel.

Step 1: Open Microsoft Excel where you want to Sum Cell with specific Color in defined range of Cell


Step 2: Using Keyboard press Alt-F11 to Open Microsoft Visual Basic for Applications


Step 3: User right click on "Project - VBAProject (pane)" select insert and click on "Module (menu item)"

Step 4: Copy the following code in Module1 (Code) (window)" and save and Close the Microsoft Visual Basic for Applications


Function SumByColor(rCellColor As Range, rRange As Range) As Double
    Dim rCell As Range
    Dim dSum As Double
    dSum = 0
    For Each rCell In rRange
        If rCell.Interior.Color = rCellColor.Interior.Color Then
            dSum = dSum + rCell.Value
        End If
    Next rCell
    SumByColor = dSum
End Function

 

 

Step 5: Now select Cell where you want sum and type =SumByColor(referenceCell,SelectRange)  

 

For my Example =SumByColor(E31,H2:H28) 
where I want to add red Color Cell and my reference Cell is E31(Sum of Wrong Entry)
 
 


Enjoy....
Next Post Previous Post
No Comment
Add Comment
comment url