+ Reply to Thread
Results 1 to 12 of 12

An alternative to SUMIF to calculate the average of cells.

  1. #1
    Registered User
    Join Date
    03-13-2010
    Location
    Bedfordshire, England
    MS-Off Ver
    Excel 2010
    Posts
    60

    An alternative to SUMIF to calculate the average of cells.

    I am currently using the following code to find an average of all the numbers in the B column that contain an MALE in the A column. Cell A50 contains the total number of MALES in column A6:A45. This falls down when there is no corresponding data in the B column for one of the males. Is there anyway of calculating this so it only divides or averages by the number of cells that contain data?



    [code]
    =SUMIF(A6:A45,"MALE",(B6:B45))/A50
    [code]
    Last edited by BobTheRocker; 09-30-2010 at 10:51 AM.

  2. #2
    Forum Guru
    Join Date
    08-05-2004
    Location
    NJ
    MS-Off Ver
    365
    Posts
    13,582

    Re: An alternative to SUMIF to calculate the average of cells.

    There might be a simpler formula but I came up with

    =SUMPRODUCT(--($A$6:$A$49="male"),$B$6:$B$49)/SUMPRODUCT(--(ISNUMBER($B$6:$B$49))*($A$6:$A$49="male"))
    Does that work for you?
    ChemistB
    My 2?

    substitute commas with semi-colons if your region settings requires
    Don't forget to mark threads as "Solved" (Edit First post>Advanced>Change Prefix)
    If I helped, Don't forget to add to my reputation (click on the little star at bottom of this post)

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

  3. #3
    Forum Moderator jeffreybrown's Avatar
    Join Date
    02-19-2009
    Location
    Cibolo, TX
    MS-Off Ver
    Office 365
    Posts
    10,327

    Re: An alternative to SUMIF to calculate the average of cells.

    You could also use...

    =AVERAGE(IF($A$6:$A$45="MALE",IF($B$6:$B$45<>"",$B$6:$B$45)))

    This is an array formula which needs to be confirmed with Ctrl + Shift + Enter.

    If entered successfully the formula will be surrounded by {}

    Do not enter {} manually...

  4. #4
    Registered User
    Join Date
    03-13-2010
    Location
    Bedfordshire, England
    MS-Off Ver
    Excel 2010
    Posts
    60

    Re: An alternative to SUMIF to calculate the average of cells.

    Spot on, I've just tested that. Ive used SUMPRODUCT before but could not get my head round it. Thats brilliant thank you.

  5. #5
    Registered User
    Join Date
    03-13-2010
    Location
    Bedfordshire, England
    MS-Off Ver
    Excel 2010
    Posts
    60

    Re: An alternative to SUMIF to calculate the average of cells.

    =SUMPRODUCT(--($A$6:$A$49="male"),$B$6:$B$49)/SUMPRODUCT(--(ISNUMBER($B$6:$B$49))*($A$6:$A$49="male"))

    Ok is this acceptable to avoid errors, or is there an easier way?

    =IF(ISERROR(SUMPRODUCT(--($A$6:$A$49="male"),$B$6:$B$49)/SUMPRODUCT(--(ISNUMBER($B$6:$B$49))*($A$6:$A$49="male"))),"NONE",=SUMPRODUCT(--($A$6:$A$49="male"),$B$6:$B$49)/SUMPRODUCT(--(ISNUMBER($B$6:$B$49))*($A$6:$A$49="male"))))

    I've used this method of IF(ISERROR many times all over my spreadsheets but I'm not sure is very efficient.

  6. #6
    Forum Moderator jeffreybrown's Avatar
    Join Date
    02-19-2009
    Location
    Cibolo, TX
    MS-Off Ver
    Office 365
    Posts
    10,327

    Re: An alternative to SUMIF to calculate the average of cells.

    Not sure your formula will work you have an extra equal sign in the middle. I do not know of a more efficient method; however, in 2007 you could incorportate the IFERROR function which would cut down on the formula.

    One of...

    =IF(ISERROR(SUMPRODUCT(--($A$6:$A$49="male"),$B$6:$B$49)/SUMPRODUCT(--(ISNUMBER($B$6:$B$49))*($A$6:$A$49="male"))),"None",SUMPRODUCT(--($A$6:$A$49="male"),$B$6:$B$49)/SUMPRODUCT(--(ISNUMBER($B$6:$B$49))*($A$6:$A$49="male")))

    or

    =IF(ISERROR(AVERAGE(IF($A$6:$A$45="MALE",IF($B$6:$B$45<>"",$B$6:$B$45)))),"None",AVERAGE(IF($A$6:$A$45="MALE",IF($B$6:$B$45<>"",$B$6:$B$45))))

    Enter with Ctrl + Shift + Enter

  7. #7
    Forum Expert daddylonglegs's Avatar
    Join Date
    01-14-2006
    Location
    England
    MS-Off Ver
    Microsoft 365
    Posts
    14,702

    Re: An alternative to SUMIF to calculate the average of cells.

    If the error you want to avoid is #DIV/0! try

    =SUMPRODUCT(ISNUMBER($B$6:$B$49)*($A$6:$A$49="male"),$B$6:$B$49)/MAX(1,SUMPRODUCT(ISNUMBER($B$6:$B$49)*($A$6:$A$49="male")))

    or

    =LOOKUP(9.9E+307,CHOOSE({1,2},0,AVERAGE(IF($A$6:$A$45="MALE",IF($B$6:$B$45<>"",$B$6:$B$45)))))

    confirmed with CTRL+SHIFT+ENTER
    Audere est facere

  8. #8
    Forum Guru DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Northumberland, UK
    MS-Off Ver
    O365
    Posts
    21,531

    Re: An alternative to SUMIF to calculate the average of cells.

    Re: SUMPRODUCT - you only need one SUMPRODUCT I think (Divisor). SUMIF should suffice as before for the first part of the equation.

    Of course if the ranges were vast it would make sense to create a "key" to negate need for Array/Sumproduct altogether.

  9. #9
    Forum Moderator jeffreybrown's Avatar
    Join Date
    02-19-2009
    Location
    Cibolo, TX
    MS-Off Ver
    Office 365
    Posts
    10,327

    Re: An alternative to SUMIF to calculate the average of cells.

    Quote Originally Posted by DonkeyOte View Post
    Of course if the ranges were vast it would make sense to create a "key" to negate need for Array/Sumproduct altogether.
    Hi Luke,

    This part lost me...What do you mean by a "key"?

    Thanks, Jeff

  10. #10
    Forum Guru DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Northumberland, UK
    MS-Off Ver
    O365
    Posts
    21,531

    Re: An alternative to SUMIF to calculate the average of cells.

    What I'm getting at is that where the data sets being processed are vast (they aren't here) it's often advisable to create a key which contains all pertinent info. such that you can conduct multi conditional tests without need for Array/SUMPRODUCT (more so preXL2007 of course)

    For ex. here we could create a key along the lines of:

    Please Login or Register  to view this content.
    Then

    Please Login or Register  to view this content.
    It follows that given COUNTIF & SUMIF can work with wildcards one key can facilitate a whole variety of different tests where some of the concatenated fields are irrelevant etc...
    (of course pending the tests creating an appropriate key may require some thought)

    Mike Girvin has posted an excellent youtube clip of this approach if interested: http://www.youtube.com/watch?v=ry3vSqN1TQ0

    This approach is something I use quite regularly on the boards on those threads related to poor performance (as they generally result from overuse of SUMPRODUCT / Arrays)

  11. #11
    Forum Moderator jeffreybrown's Avatar
    Join Date
    02-19-2009
    Location
    Cibolo, TX
    MS-Off Ver
    Office 365
    Posts
    10,327

    Re: An alternative to SUMIF to calculate the average of cells.

    Thanks Luke I understand now.

  12. #12
    Forum Expert daddylonglegs's Avatar
    Join Date
    01-14-2006
    Location
    England
    MS-Off Ver
    Microsoft 365
    Posts
    14,702

    Re: An alternative to SUMIF to calculate the average of cells.

    Quote Originally Posted by DonkeyOte View Post
    Re: SUMPRODUCT - you only need one SUMPRODUCT I think (Divisor). SUMIF should suffice as before for the first part of the equation.
    Correct - thanks Luke, I came over all SUMPRODUCT there, my suggestion could be just

    =SUMIF($A$6:$A$49,"male",$B$6:$B$49)/MAX(1,SUMPRODUCT(ISNUMBER($B$6:$B$49)*($A$6:$A$49="male")))

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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