+ Reply to Thread
Results 1 to 7 of 7

Average if color condition is met

  1. #1
    Forum Contributor
    Join Date
    12-02-2008
    Location
    Brisbane
    MS-Off Ver
    2016
    Posts
    811

    Average if color condition is met

    Is it possible to average a row only if a color condition is met (example on the attached spreadsheet)?
    Attached Files Attached Files

  2. #2
    Forum Expert Paul's Avatar
    Join Date
    02-05-2007
    Location
    Wisconsin
    MS-Off Ver
    2016/365
    Posts
    6,887
    Hi again, Bob.

    There's no built-in function in Excel that will do this, but a UDF will do the trick. For example, place the following function into a standard Module in the VB Editor:
    Please Login or Register  to view this content.
    Then use the following formula in any cell:
    =AvgColors(C8:G8)

    The code loops through the cells in the range you specify, and if the cell in row 7 of any column is green (colorindex=50 using your example spreadsheet), then the code adds that cell value to the sum, and increases the count of cells by 1. At the end, as long as more than one cell had a green background in row 7, the average is displayed (otherwise zero is returned).

  3. #3
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689
    You might instead use a formula that averages based on the criteria on which the cells are colored.
    Entia non sunt multiplicanda sine necessitate

  4. #4
    Forum Contributor
    Join Date
    12-02-2008
    Location
    Brisbane
    MS-Off Ver
    2016
    Posts
    811

    Thanks and can you expand

    Thanks (again) for that, Paul

    SMG, can you expand on what you mean? Soryy, I'm a newbie to all of this.

    Thanks

  5. #5
    Forum Contributor
    Join Date
    12-02-2008
    Location
    Brisbane
    MS-Off Ver
    2016
    Posts
    811

    #NAME? error

    Hi Paul

    I'm getting a #NAME? error which i think means the spreadsheet can't find the declaration (?). If I right click on the sheet tab I can see the code I pasted into VB

    Where am I going wrong?

  6. #6
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689
    Quote Originally Posted by shg
    You might instead use a formula that averages based on the criteria on which the cells are colored.
    Quote Originally Posted by BB
    can you expand on what you mean?
    Assuming the cells are not colored at random, there is some formula that can be entered in, say, row 6, that returns TRUE if the column is to be included in the average.

    Then you can use the results in that row to both color the cells (using conditional formatting) and to control the averaging formula; the formula in I8 and copied down could be

    =SUMIF(C$6:G$6, TRUE, C8:G8) / COUNTIF(C$6:G$6, TRUE)

    Philosophically, the cells are colored for a reason; that same reason (and not the symptom of color) should be used for the formula. And if you decide you prefer a different highlight color, or to highlight using boldface print, you only have to change one thing (the CF) and not two.
    Last edited by shg; 12-12-2008 at 12:27 PM.

  7. #7
    Forum Expert Paul's Avatar
    Join Date
    02-05-2007
    Location
    Wisconsin
    MS-Off Ver
    2016/365
    Posts
    6,887
    Bob, make sure the code is in a standard module, not a worksheet module (in the VB Editor, click Insert -> Module).

    Shg's recommendation may also work if you can incorporate it into your workbook.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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