+ Reply to Thread
Results 1 to 12 of 12

An alternative to SUMIF to calculate the average of cells.

Hybrid View

  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,800

    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

+ 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