+ Reply to Thread
Results 1 to 11 of 11

Excel 2008 : Can I nest an IF function in a SUMPRODUCT function

  1. #1
    Forum Contributor
    Join Date
    09-08-2010
    Location
    Australia
    MS-Off Ver
    Excel 2010
    Posts
    125

    Can I nest an IF function in a SUMPRODUCT function

    I have Excel 2010. In the formula below I need to sort rows of data by conditions in the columns. In the bold section below I have tried to nest an IF function in a SUMPRODUCT function. I get the correct answers in the fx box but it displays #VALUE! in the cell.

    What I need to do is filter rows by the value in a reference cell or if no value in the reference cell don't filter the column (Include all rows with data in that column). I need to do this with several more columns.


    =SUMPRODUCT(--IF(AH$31>0,(F$6:F$204=AH$31),(F$6:F$204>0)),--(E$6:E$204+((E$6:E$204<AH$23)*(AH$23>AH$24))>=AH$23),--($E$6:$E$204+(($E$6:$E$204<AH$23)*(AH$23>AH$24))<=(AH$24+(AH$23>AH$24))),--(G$6:G$204=AI31))
    Last edited by ccpsc; 11-06-2010 at 01:46 AM.

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

    Re: Can I nest an IF function in a SUMPRODUCT function

    When you embed an IF into a SUMPRODUCT invariably it necessitates Array entry ... this is why for ex. you get the correct answer in fx but not in cell - the former evaluates as an Array whereas the latter is not (presently).

    Goes without saying that if you use SUMPRODUCT with Array entry then generally speaking you may as well just use an Array as it's more flexible than SUMPRODUCT
    (the advantage of SUMPRODUCT is that it doesn't require Array entry - it's not really any more efficient)

    Without seeing a sample it's hard to know if a SEARCH approach would suffice but in the meantime the below should work I think:

    Please Login or Register  to view this content.
    the above [in essence an OR(AND(),AND()) and the SIGN is used to avoid double counting] and is to replace the below in full:

    Please Login or Register  to view this content.
    So you end up with:

    Please Login or Register  to view this content.
    note I removed some superfluous double unary coercion

  3. #3
    Forum Contributor
    Join Date
    09-08-2010
    Location
    Australia
    MS-Off Ver
    Excel 2010
    Posts
    125

    Re: Can I nest an IF function in a SUMPRODUCT function

    The full code didn't work but I coppied in your piece of code into the function and it worked. I like the double uraries as they seperate the code and make it easier to read.

    As you can see below there are seven columns to filter. This template is used for many times and not all the columns are allways required. If I leave a column blank the function won't work because it filters out all the rows.

    To get around this I have entered an "a" in each row of any unused columns and either ented an "a" in the reference cell or just left it blank. Can this be corrected so I don't have to enter anything in unused columns? I hope this makes sense.


    =SUMPRODUCT(--SIGN(((F$6:F$204=AH$25)*(AH$25>0))+((F$6:F$204>0)*(AH$25=0))),--SIGN(((G$6:G$204=AH$26)*(AH$26>0))+((G$6:G$204>0)*(AH$26=0))),--SIGN(((H$6:H$204=AH$27)*(AH$27>0))+((H$6:H$204>0)*(AH$27=0))),--SIGN(((I$6:I$204=AH$28)*(AH$28>0))+((I$6:I$204>0)*(AH$28=0))),--SIGN(((J$6:J$204=AH$29)*(AH$29>0))+((J$6:J$204>0)*(AH$29=0))),--SIGN(((K$6:K$204=AH$30)*(AH$30>0))+((K$6:K$204>0)*(AH$30=0))),--SIGN(((L$6:L$204=AH$31)*(AH$31>0))+((L$6:L$204>0)*(AH$31=0))),--(E$6:E$204+((E$6:E$204<AH$23)*(AH$23>AH$24))>=AH$23),--($E$6:$E$204+(($E$6:$E$204<AH$23)*(AH$23>AH$24))<=(AH$24+(AH$23>AH$24))))

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

    Re: Can I nest an IF function in a SUMPRODUCT function

    Quote Originally Posted by ccpsc View Post
    The full code didn't work
    Post a sample to illustrate.

    Quote Originally Posted by ccpsc
    Can this be corrected so I don't have to enter anything in unused columns? I hope this makes sense.
    Post a sample.

    Quote Originally Posted by ccpsc
    I like the double uraries as they seperate the code and make it easier to read.
    So be it but you are performing needless calculations.

    edit: point re: providing sample is: yes - I am pretty sure we can improve the formula (and add flexibility) but though we can interpret what you're doing it's much easier to see in action.
    Last edited by DonkeyOte; 11-06-2010 at 09:50 AM.

  5. #5
    Forum Contributor
    Join Date
    09-08-2010
    Location
    Australia
    MS-Off Ver
    Excel 2010
    Posts
    125

    Re: Can I nest an IF function in a SUMPRODUCT function

    Formula is in cell V24. I didn't think the double unaries would be a problem as there will never be any more then a couple of hundred entries in any file.
    Attached Files Attached Files

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

    Re: Can I nest an IF function in a SUMPRODUCT function

    If we assume you need to be able to cater for time period of interest to fluctuate between same day and overnight (thereby precluding single COUNTIFS) then one possibility to account for blanks etc...
    [ie K & L can be blank - no need for "a"]

    Please Login or Register  to view this content.
    the above would return 2 per your sample with T30:T31 left blank
    (were you to clear say T25 the result would be 4)
    Last edited by DonkeyOte; 11-07-2010 at 04:46 AM. Reason: modified final search range per post # 9

  7. #7
    Forum Contributor
    Join Date
    09-08-2010
    Location
    Australia
    MS-Off Ver
    Excel 2010
    Posts
    125

    Re: Can I nest an IF function in a SUMPRODUCT function

    That works fine, thanks DonkeyOte. How does ISNUMBER(SEARCH($AH$25,$F$6:$F$204&"^") work inparticular the "^"?
    Last edited by ccpsc; 11-06-2010 at 08:39 PM.

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

    Re: Can I nest an IF function in a SUMPRODUCT function

    First there appears to be a typo in my prior suggestion - the last SEARCH range should refer to Col L rather than K (which is repeated twice over)

    Quote Originally Posted by ccpsc
    How does ISNUMBER(SEARCH($AH$25,$F$6:$F$204&"^") work
    SEARCH will return either a number (the char position of the criteria in the searched string) or #VALUE! error (not found)

    It follows that an ISNUMBER will convert the above to a Boolean - TRUE (found) or FALSE (not found).

    Given the above we need not apply ISNUMBER to each SEARCH result - we can simply try adding all SEARCH results together and then test the final result.
    (TRUE only if no one SEARCH returned #VALUE!)

    Quote Originally Posted by ccpsc
    ...inparticular the "^"?
    Given you have blanks at source and wish for blank criteria value (or *) to equate to "all ok" you can tweak the source values by adding ^ to each.
    The addition of the ^ does not affect the search where criteria is specified [note 1] but ensures no source value is truly blank when evaluated.
    Per your "a" the SEARCH will always return a number when criteria is blank (or *)


    note 1: we select a char that would never:

    a) appear in criteria string
    and
    b) appear in the strings to be Searched

  9. #9
    Forum Contributor
    Join Date
    09-08-2010
    Location
    Australia
    MS-Off Ver
    Excel 2010
    Posts
    125

    Re: Can I nest an IF function in a SUMPRODUCT function

    Yes I found the error regarding L & K columns and fixed it thanks. And thanks for the explaination.

    It is all working very good but as you pointed out when the filters are empty it counts all the records due to "^", 199 instead of 4. I can get around this by deleting the unused rows each time. Is there a way to fix it besides deleting the rows?
    Attached Files Attached Files

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

    Re: Can I nest an IF function in a SUMPRODUCT function

    Given the blank filters are handled in that manner for a good reason all you need do to exclude blank rows is validate the Time.

    You can do this very simply by adding an ISNUMBER test:

    Please Login or Register  to view this content.
    If you add the above to your SUMPRODUCT you will get your 4.

  11. #11
    Forum Contributor
    Join Date
    09-08-2010
    Location
    Australia
    MS-Off Ver
    Excel 2010
    Posts
    125

    Re: Can I nest an IF function in a SUMPRODUCT function

    Thank you very much it all works very well.

+ 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