+ Reply to Thread
Results 1 to 7 of 7

Excel 2007 : Getting the average value when source data includes blanks?

  1. #1
    Valued Forum Contributor
    Join Date
    06-10-2011
    Location
    London, England
    MS-Off Ver
    Excel 2010
    Posts
    341

    Getting the average value when source data includes blanks?

    Hi,

    I was wondering how I should modify my code so that it will reliably calculate average values without throwing me DIV/0 errors for data that is all blanks or returning a blank to me because my formula isn't written very well

    =AVERAGEIFS(Sheet1!$H:$H,Sheet1!$AS:$AS,B$1,Sheet1!$D:$D,$A2),"")
    This formula will give me the DIV/0 error

    =IF(COUNTIFS(Sheet1!$AS:$AS,B$1,Sheet1!$D:$D,$A2,Sheet1!$H:$H,NOT("")),AVERAGEIFS(Sheet1!$H:$H,Sheet1!$AS:$AS,B$1,Sheet1!$D:$D,$A2),"")
    This formula gives me only blanks

    The range of values in Sheet1!H:H is from 0-10 and blanks. The formula needs to reliably exclude blanks completely.

    Also, can I express my formula in any simpler shape or form?

    Thanks
    Last edited by dip11; 12-06-2011 at 01:20 PM.

  2. #2
    Valued Forum Contributor tlafferty's Avatar
    Join Date
    04-08-2011
    Location
    United States, Tacoma, WA
    MS-Off Ver
    Excel 2010, Excel 2013 Customer Preview
    Posts
    1,112

    Re: Getting the average value when source data includes blanks?

    =SUMPRODUCT(--(Sheet1!$H:$H>0)*(Sheet1!$H:$H<>"")*(Sheet1!$H:$H))/SUMPRODUCT(--(Sheet1!$H:$H>0)*(Sheet1!$H:$H<>"") This will give the average of all cells in H:H that are not an empty string and are greater than zero. The first two arrays return trues and falses which are converted to 1 or 0. The resulting array is then multiplied by values in the H column so that only values meeting the criteria are returned. This result is divided by the same array in the second sumproduct formula but the results are not multiplied by the values, so you are getting the sum of all divided by the count. You can then tweak for the AS portion.
    If your question has been satisfactorily addressed, please consider marking it solved. Click the Thread Tools dropdown and select Mark thread as solved.
    Also, you might want to add to the user's reputation by clicking the star icon in the lower left corner of the post with the answer- it's why we do what we do...

    Thomas Lafferty
    Analyst/Programmer

  3. #3
    Forum Expert tigeravatar's Avatar
    Join Date
    03-25-2011
    Location
    Colorado, USA
    MS-Off Ver
    Excel 2003 - 2013
    Posts
    5,361

    Re: Getting the average value when source data includes blanks?

    Sumproduct is backwards compatible, but calculates as an array formula so it can bog things down when you use whole columns. I saw you were using AverageIfs which is an Excel 2007+ formula, so the following should also work:

    =IFERROR(AVERAGEIFS(Sheet1!$H:$H,Sheet1!$D:$D,$A2,Sheet1!$AS:$AS,B$1,Sheet1!$H:$H,"<>"),0)

    It will average non-blank cells cells in column H for which D and AS meet criteria. If there is nothing that meets critieria, the output will be a 0.
    Hope that helps,
    ~tigeravatar

    Forum Rules: How to use code tags, mark a thread solved, and keep yourself out of trouble

  4. #4
    Valued Forum Contributor
    Join Date
    06-10-2011
    Location
    London, England
    MS-Off Ver
    Excel 2010
    Posts
    341

    Re: Getting the average value when source data includes blanks?

    Thanks very much for your input, worked great

  5. #5
    Forum Expert tigeravatar's Avatar
    Join Date
    03-25-2011
    Location
    Colorado, USA
    MS-Off Ver
    Excel 2003 - 2013
    Posts
    5,361

    Re: Getting the average value when source data includes blanks?

    Private message from dip11:
    Quote Originally Posted by dip11
    One question I had in regards to your code was what <> actually does? I tried removing it and the results were the same.

    For reference:
    =IFERROR(AVERAGEIFS(Sheet1!$H:$H,Sheet1!$D:$D,$A2,Sheet1!$AS:$AS,B$1,Sheet1!$H:$H,"<>"),0)

    Thanks
    The <> tells Excel "Does not equal", so "<>" means, "Does not equal nothing", or in other words, it tells excel to ignore cells that are blank.

    Now for the ugly part. From the forum rules (link in my sig): Don't Private Message or email questions to moderators or other members. The point of having a public forum is to share solutions to common (and sometimes uncommon) problems with all members.

  6. #6
    Valued Forum Contributor
    Join Date
    06-10-2011
    Location
    London, England
    MS-Off Ver
    Excel 2010
    Posts
    341

    Re: Getting the average value when source data includes blanks?

    Ah I see.
    So since my results looked identical with or without the <> portion, would the code run more efficiently (faster) with or without the <> part?

    Sorry for the PM then, didn't know that.

  7. #7
    Forum Expert tigeravatar's Avatar
    Join Date
    03-25-2011
    Location
    Colorado, USA
    MS-Off Ver
    Excel 2003 - 2013
    Posts
    5,361

    Re: Getting the average value when source data includes blanks?

    If the results are identical for you with or without that part, try leaving out the last two arguments of the averageifs formula. That would allow excel to calculate it a bit a faster:
    =IFERROR(AVERAGEIFS(Sheet1!$H:$H,Sheet1!$D:$D,$A2,Sheet1!$AS:$AS,B$1),0)

    If that does not yield the desired results, then leave the last two arguments in. With or without the <> inside the quotes, the formula will calculate at pretty much the same rate.

+ 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