+ Reply to Thread
Results 1 to 6 of 6

Sum and average cells based on background color

  1. #1
    Registered User
    Join Date
    08-22-2013
    Location
    Oshkosh, Wisconsin
    MS-Off Ver
    Excel 2007
    Posts
    3

    Sum and average cells based on background color

    I have a spreadsheet of data that has been categorized by cell background color. I need to find the total sum and total averages for various data types on the spreadsheet. Right now, I have a macro set up that sums data based on the cells background color, but I can't figure out how to average cells based on color so I've been manually doing that. Is there a way I can use a macro to take the sum of certain data types and average other data types on the same tab? Below is the current macro I'm using:

    Function ColorFunction(rColor As Range, rRange As Range, Optional SUM As Boolean)
    Dim rCell As Range
    Dim lCol As Long
    Dim vResult
    lCol = rColor.Interior.ColorIndex
    If SUM = True Then
    For Each rCell In rRange
    If rCell.Interior.ColorIndex = lCol Then
    vResult = WorksheetFunction.SUM(rCell, vResult)
    End If
    Next rCell
    Else
    For Each rCell In rRange
    If rCell.Interior.ColorIndex = lCol Then
    vResult = 1 + vResult
    End If
    Next rCell
    End If
    ColorFunction = vResult
    End Function

    I'm new to macros, so any and all help/advice is appreciated!! Thank you!

  2. #2
    Forum Expert
    Join Date
    01-12-2007
    Location
    New Jersey
    Posts
    2,127

    Re: Sum and average cells based on background color

    Hi Kaitlynn,

    Looking at your code, it seems that it has two options built into it. Option TRUE sums the range, and option FALSE counts the range. Using that logic, the average can be determined by dividing the sum by the count.

    For example,

    =ColorFunction(A1,B1:B10,TRUE)/ColorFunction(A1,B1:B10,FALSE)

    Obviously, without your data, I can't test to see if it works correctly. Try it out and let us know if that's whats needed

  3. #3
    Forum Expert millz's Avatar
    Join Date
    08-14-2013
    Location
    Singapore
    MS-Off Ver
    Excel, Access 2016
    Posts
    1,694

    Re: Sum and average cells based on background color

    Please Login or Register  to view this content.

  4. #4
    Registered User
    Join Date
    08-22-2013
    Location
    Oshkosh, Wisconsin
    MS-Off Ver
    Excel 2007
    Posts
    3

    Re: Sum and average cells based on background color

    Thanks for the responses! millz, I tried your code but it seems to be averaging all the data in the selected range, instead of only the cells with a specific color? I attached a sample of the original data; the column titled "Cost Per 1,000 People Reached" is the data that needs to be averaged. Thanks for the help!
    Attached Files Attached Files

  5. #5
    Forum Expert millz's Avatar
    Join Date
    08-14-2013
    Location
    Singapore
    MS-Off Ver
    Excel, Access 2016
    Posts
    1,694

    Re: Sum and average cells based on background color

    Hi, I tried the function I've pasted and it seems to be working fine? Check range F33:F36
    Attached Files Attached Files

  6. #6
    Registered User
    Join Date
    08-22-2013
    Location
    Oshkosh, Wisconsin
    MS-Off Ver
    Excel 2007
    Posts
    3

    Re: Sum and average cells based on background color

    I must have done something wrong. THANK YOU!!

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Color Index to sum up cells based on background color
    By jph89 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 04-16-2013, 03:23 PM
  2. [SOLVED] Macro to change all cells with a certain background color in another background color
    By kevinvzandvoort in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 06-13-2012, 11:04 AM
  3. Replies: 3
    Last Post: 05-02-2012, 09:08 AM
  4. Count Cells Based On A Background Color
    By NSTurk725 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 04-27-2010, 11:29 AM
  5. Replies: 2
    Last Post: 04-27-2006, 01:46 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1