+ Reply to Thread
Results 1 to 7 of 7

Sum If conditin based on Array criteria

  1. #1
    Registered User
    Join Date
    07-16-2009
    Location
    Arkansas
    MS-Off Ver
    Excel 2016
    Posts
    9

    Sum If conditin based on Array criteria

    I have an formula i did two different ways that ads up amounts in a column an item in an array matches an item in the match column.

    array formula
    {=SUM(IF( ( {"2","b3","a23","3"}=TEXT(C11:C19,"#") ),D11:D19, 0))}

    standard sumproduct
    =SUMPRODUCT(({"2","b3","a23","3"}=TEXT($C$12:$C$19,"#"))*--($D$12:$D$19))

    However I would like to take the array part {"2","b3","a23","3"} out and reference the array as a value in a cell B2. So that Cell B2 had either {"2","b3","a23","3"} or "2","b3","a23","3"


    =SUM(IF( ( INDEX( B2,0,0)=TEXT(C11:C19,"#") ),D11:D19, 0))
    =SUMPRODUCT((INDEX(B2,0,0) =TEXT($C$12:$C$19,"#"))*--($D$12:$D$19))

    However those formulas don't work.

    I have successfully used the index function in the past, but this did not work. Using the Evaluate Formula option I see it added Extra quotes around every item including the braces "{" and "}". Index by itself does not add the extra quotes.

    Is there a trick as I want to use variations of this formula several times and want just one array reference.

    any an all thoughts are appreciated.

    thanks

    Alan

  2. #2
    Forum Expert Tony Valko's Avatar
    Join Date
    12-31-2011
    Location
    Pittsburgh
    MS-Off Ver
    2002, 2007:2013
    Posts
    18,890

    Re: Sum If conditin based on Array criteria

    Curious as to why you use this...

    TEXT($C$12:$C$19,"#")

    What exactly is in C12:C19?

    You should put the array items {"2","b3","a23","3"} into separate cells.

    Also, quoting numbers converts them into TEXT values.

    "2" and 2 are not the same. "2" is a text value while 2 is a numeric value.

    We may be able to come up with something better if you could post the file so we can see what you're trying to do.
    Biff
    Microsoft MVP Excel
    Keep It Simple Stupid

    Let's Go Pens. We Want The Cup.

  3. #3
    Registered User
    Join Date
    07-16-2009
    Location
    Arkansas
    MS-Off Ver
    Excel 2016
    Posts
    9

    Re: Sum If conditin based on Array criteria

    Thanks for the fast reply.

    I was doing TEXT($C$12:$C$19,"#") to force the criteria range to be text so that "2" would equal to "2" and not 2. I tried things both ways and this seemed cleaner at the time. That part of my process seemed good.

    I would prefer the range to be in an array with braces in a single cell like {a, b, c} and not having a, b, c in different cells as the number of critera for my various sums will change so i might have 3 items to 10 items so doing the single array seemed "safer".

    Here is a link to a sample file of what I was trying
    https://www.dropbox.com/s/ukim6y8q1z...arry.xlsx?dl=0

  4. #4
    Forum Expert newdoverman's Avatar
    Join Date
    02-07-2013
    Location
    Port Dover, Ontario, Canada
    MS-Off Ver
    2010
    Posts
    10,330

    Re: Sum If conditin based on Array criteria

    This should work for you
    Formula: copy to clipboard
    Please Login or Register  to view this content.
    <---------If you like someone's answer, click the star to the left of one of their posts to give them a reputation point for that answer.
    Ron W

  5. #5
    Forum Expert Tony Valko's Avatar
    Join Date
    12-31-2011
    Location
    Pittsburgh
    MS-Off Ver
    2002, 2007:2013
    Posts
    18,890

    Re: Sum If conditin based on Array criteria

    Doing it that way makes it orders of magnitude more complicated.

    Enter the criteria as a comma space delimited text string as shown in cells B2:B3.

    Data Range
    A
    B
    C
    D
    1
    2
    Group1
    2, b3, a23, 3
    641
    3
    Group2
    a34, 1, 12, b009
    159
    10
    11
    id
    name
    pd
    amount
    12
    9009
    john
    2
    32
    13
    9008
    sally
    b3
    43
    14
    9007
    bill
    a34
    5
    15
    9009
    john
    b3
    23
    16
    9008
    sally
    3
    543
    17
    9007
    bill
    12
    23
    18
    9009
    john
    1
    34
    19
    9008
    sally
    b009
    54
    20
    9007
    bill
    a34
    43


    Then, this formula entered in C2 and copied down:

    =SUMPRODUCT(SUMIF(C$12:C$20,INDEX(TRIM(MID(SUBSTITUTE(B2,", ",REPT(" ",99)),
    (99*(ROW(INDIRECT("1:"&LEN(B2)-LEN(SUBSTITUTE(B2,",",""))+1))-1))+1,99)),0),D$12:D$20))

    That formula is doing the same thing as this:

    =SUMIF(C$12:C$20,{2,"b3","a23",3},D$12:D$20)

    If you were to put each criteria in separate cells...

    Data Range
    F
    G
    H
    I
    J
    2
    2
    3
    B3
    A23
    641
    3
    1
    12
    A34
    B009
    159
    10
    ------
    ------
    ------
    ------
    ------


    This formula entered in J2 and copied down:

    =SUMPRODUCT(SUMIF(C$12:C$20,F2:I2,D$12:D$20))

    I would put the criteria in separate cells and use the simple formula.

  6. #6
    Registered User
    Join Date
    07-16-2009
    Location
    Arkansas
    MS-Off Ver
    Excel 2016
    Posts
    9

    Re: Sum If conditin based on Array criteria

    Tony,

    Wow I did not realize the formula would be so crazy. It seems like if index(b2,0,0) returns as nice array {....} that it should do it inside the other formulas instead of returning everything including the quotes in braces. The sheet I had was a sample test sheet. and the criteria range was going to be on another sheet. I guess I will rework it more to use your nice clean formula and have all the codes in their own cells. I am assuming that having extra blank cells referenced in the criteria won't break anything i.e. referencing F2:z2 instead of F2:i2 =SUMPRODUCT(SUMIF(C$12:C$20,F2:z2,D$12:D$20)


    I really appreciate your response to this. Just wish index inside the others acted differently. I have used Index for other things and had what needed and liked it.

    thanks

    Alan

  7. #7
    Forum Expert Tony Valko's Avatar
    Join Date
    12-31-2011
    Location
    Pittsburgh
    MS-Off Ver
    2002, 2007:2013
    Posts
    18,890

    Re: Sum If conditin based on Array criteria

    INDEX(B2,0,0)

    Is returning the value of cell B2 which is the TEXT string: {2,"b3","a23",3}. It's not a real array constant.

    Here's how it would be implemented in a formula:

    =SUMPRODUCT(SUMIF(C12:C20,INDEX(B2,0,0),D12:D20))

    To see how Excel evaluates it select the formula cell...

    In the Formula Bar highlight this portion INDEX(B2,0,0) and press function key F9.

    It will appear as "{2,""b3"",""a23"",3}", which is a TEXT string and not the array we hoped for.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. [SOLVED] Dynamic Changing Data, Based on Criteria Store in Array, Sum array and store in Cell
    By penbeacho in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 07-28-2015, 10:31 AM
  2. [SOLVED] Set autofilter criteria based on changing array
    By JasperD in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 04-15-2015, 11:16 AM
  3. Auto Populate based on 2 criteria using Array
    By lexusap in forum Excel Formulas & Functions
    Replies: 0
    Last Post: 03-23-2015, 02:56 AM
  4. Sum array (or something) based on multiple criteria
    By VBA-NOOB in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 02-25-2015, 05:02 PM
  5. [SOLVED] splitting an array into columns based on criteria
    By akamenov88 in forum Excel Formulas & Functions
    Replies: 8
    Last Post: 07-14-2014, 01:55 PM
  6. Select Array Based on Criteria
    By excelsf in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 09-21-2010, 06:04 PM
  7. Find last entry in an array based on 1 criteria
    By ryanh114 in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 08-09-2010, 12:23 PM

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