WISDOMBYDATA
  • BLOG
    • Blog Guide
    • Blog History
  • EXCEL
    • Functions & Formulas
    • VBA & Macros
    • VLOOKUP
    • Pivot Tables
    • Conditional Formatting
    • Tricks & Shortcuts
  • BI
    • SAP BOBJ/BW
    • Tableau
  • SQL
  • ABOUT
    • About WBD
    • About Me

Sum cells in MS Excel based on background color

4/30/2016

2 Comments

 

The VBA feature of MS Excel makes it possible to sum cells based on their background color. Summing cells based on background color could save lots of time and add a lot of value to the task at hand.
Consider the example below. 
Picture
The table above demonstrates all annual healthcare expenses for an individual. The cells that have a light green back ground color are the expenses that the individual paid themselves but eventually got reimbursed for by their insurance firm. As you could see the last row of the table sums up all expenses for each month irregardless of the background color of the cells. 

Here are four simple steps that would allow us to do the sum based on the background of the cells so that we could know how much of the expenses have been covered by insurance and what amount hasn’t.

1. Go to the "Developer" tab and left click on “Visual Basic”. If your developer tab is not visible click here to learn how to activate it.
Picture
2. ​While at the VBA screen, select “Insert” and the left click on “Module”
Picture
3. Copy and paste the code below in the code screen:
​
​Function SumByColor(CellColor As Range, rRange As Range)
Dim cSum As Long
Dim ColIndex As Integer
ColIndex = CellColor.Interior.ColorIndex
For Each cl In rRange
    If cl.Interior.ColorIndex = ColIndex Then
        cSum = WorksheetFunction.Sum(cl, cSum)
    End If
Next cl
SumByColor = cSum
End Function
Picture
4. Return to the excel tab and type the following formula. and try using the "SumByColor" formula as demonstrates below:
Picture
In the screenshot above the SumByColor formula is activated and the first cell used in the formula is a reference to the background color that you wish to be summed. I chose cell $B$11 as the reference. the purpose of the dollar sign ($) is so that the reference wouldn't change after dragging the formula to other cells. As you could see the only amount that has been summed for the month of January using the SumByColor sum statement is the amount that has the light green background color. 

​Here is how the table would look like once the formula is applied for all 12 months.
Picture

2 Comments

    Categories

    All
    BI
    EXCEL
    MISC
    SQL

    Archives

    June 2020
    May 2020
    April 2020
    March 2020
    February 2020
    December 2019
    November 2019
    October 2019
    September 2019
    August 2019
    July 2019
    June 2019
    May 2019
    April 2019
    March 2019
    February 2019
    January 2019
    December 2018
    November 2018
    October 2018
    September 2018
    August 2018
    July 2018
    June 2018
    May 2018
    April 2018
    March 2018
    September 2017
    August 2017
    July 2017
    June 2017
    May 2017
    April 2017
    March 2017
    February 2017
    January 2017
    December 2016
    November 2016
    October 2016
    September 2016
    August 2016
    July 2016
    June 2016
    May 2016
    April 2016
    March 2016
    February 2016
    May 2015
    April 2015
    March 2015
    February 2015
    January 2015
    December 2014
    November 2014
    October 2014
    September 2014
    August 2014
    April 2014
    March 2014
    February 2014
    January 2014
    December 2013
    November 2013

Picture
  • BLOG
    • Blog Guide
    • Blog History
  • EXCEL
    • Functions & Formulas
    • VBA & Macros
    • VLOOKUP
    • Pivot Tables
    • Conditional Formatting
    • Tricks & Shortcuts
  • BI
    • SAP BOBJ/BW
    • Tableau
  • SQL
  • ABOUT
    • About WBD
    • About Me