return values from multiple matching rows

前端 未结 3 1211
野的像风
野的像风 2020-12-10 21:48

First off, I\'d like to do this without VB if possible, so I don\'t have to go through the hassle of teaching recipients how to enable macros.

Now, I believ

相关标签:
3条回答
  • 2020-12-10 22:28

    OK I figured out a way to get my desired results. It isn't the cleanest or best way, but it achieves my goal of listing the results horizontally, and avoids macros or pivot tables.

    I use a hidden worksheet to list out all the pos and sts values, concatenated as a single value. So...

    sts   | pos   | bye
    ----------------------
    2     | QB    | 8
    2     | RB    | 5
    2     | QB    | 11
    0     | WR    | 7
    . . .
    

    ...becomes....

         D   | E
        -----------
    5  | 2QB | 8
    6  | 2RB | 5
    7  | 2QB | 11
    8  | 0WR | 7
         . . .
    

    Then, I have a "shadow" results area that mimics the results area on my front-page worksheet. It looks like so:

         G   | H   | I   | J   | K
        -----------------------------
    5  | QB  |     |     |     |     |
    6  | RB  |     |     |     |     |
    7  | WR  |     |     |     |     | . . .
    

    In H5:H7, I have the following formula:

    =IFERROR((ADDRESS(MATCH("2"&$G5,$D$5:$D$305,0)+4,COLUMN($E5),4)),"")
    

    This returns the first cell reference it finds in the concatenated column that starts with 2 and ends with the value in column G (e.g. the formulas in row 5 are looking for "2QB").

    Then, in I5:n7 I have the following modified formula:

    =IFERROR(ADDRESS(MATCH("2"&$G5,INDIRECT(ADDRESS(ROW(INDIRECT(H5))+1,4)&":$d$"&MAX(305,ROW(INDIRECT(H5))+1)),0)+ROW(INDIRECT(H5)),COLUMN($E5),4),"")
    

    The reason I modify the subsequent columns is to change the range in which the formula is looking for its value to start at the next row after the previously found value. For example, with the data above, the formula in H5 would look for "2QB" in D5:D*n*, and return the first row it finds and attach it to column E, which would be E5.

    The formula in I5 would then look for "2QB" starting in D*6* instead of D5, a row after the row referenced in H5's result.

    Hopefully that made sense.

    So what I end up with in my hidden worksheet is this:

         G   | H   | I   | J   | K
        -----------------------------
    5  | QB  | E5  | E7  |     |     |
    6  | RB  | E6  |     |     |     |
    7  | WR  |     |     |     |     | . . .
    

    Then, on my front page worksheet, I simply get the values (the bye) referenced by the cells in H5:*n*7 using:

    =IFERROR(INDIRECT(lookups!H5),"")
    

    ...which gives me my final result:

         G   | H   | I   | J   | K
        -----------------------------
    5  | QB  | 8   | 11  |     |     |
    6  | RB  | 5   |     |     |     |
    7  | WR  |     |     |     |     | . . .
    

    Like I said, it's totally convoluted, but it works, and I can always refine it later if I figure out how. :) Thanks to you who took a swing at this seemingly complex problem for me! I'm sure your answers work beautifully as well.

    0 讨论(0)
  • 2020-12-10 22:36

    This sounds like a job for pivot tables and go to special. You need to add an ID # column though. Here is one way you could do it:

    PivotTableSolution1

    PivotTableSolution2

    Then once the blanks are selected using go to special you just need to delete them and shift cells left. PivotTableSolution3

    Good Luck.

    0 讨论(0)
  • 2020-12-10 22:47

    Something along the lines of the following could be used:

    {=INDEX(tbl, SMALL(IF(COUNTIF(G$3, $A$2:$A$9)
     *COUNTIF(G$4, $B$2:$B$9), ROW(tbl)-MIN(ROW(tbl))+1), ROW($C1)), COLUMN($C1))}
    

    where the A column is sts, B column is pos, and C column is bye. The variable tbl is the range of data (not the headers). G$3 would be the sts filter and G$4 is the pos filter.

    Copy the array formula DOWN to find all the matching byes; #NUM! will appear after finding no more matches. If this bothers your users, you can add an ISERROR or a tricky conditional format that makes the text white on white.

    You can then copy over the formula to the next column and enter new filter values.

                G   H
    sts Search  2   2
    pos Search  QB  WR
    
                10  3
                7   9
                5   #NUM!
                #NUM!#NUM!
    

    If your users are comfortable with pivot tables, using them would be much easier, I would think.

    EDIT Making the formula "transpose" is a bit tricky and I am not having any breakthrough on how to fix that. However, if you want to manually edit the column formulas, here is what you want.

    (I made the assumption that S is the sts filter. Maybe you're just doing a count there, but I didn't see where you enter the filter for sts. If S isn't the sts filter, update the formulas to point to where sts is 2 or whatever.)

    U2:

    {=INDEX(tbl, SMALL(IF(COUNTIF($S2, $B$2:$B$303)*COUNTIF($R2, $D$2:$D$303), 
     ROW(tbl)-MIN(ROW(tbl))+1), ROW($D$1)), COLUMN($D$1))}
    

    V2:

    {=INDEX(tbl, SMALL(IF(COUNTIF($S2, $B$2:$B$303)*COUNTIF($R2, $D$2:$D$303), 
    ROW(tbl)-MIN(ROW(tbl))+1), ROW($D$2)), COLUMN($D$2))}
    

    etc.

    This allows the cells to be copied down.

    I am sure there is a way to INDIRECT the ROW/COLUMN, but I ran out of time to look at this at the moment.

    EDIT 2 If you put a simple number increment somewhere, let's say U1 has 1, V1 has 2, W1 has 3, etc., you could use the following:

    {=INDEX(tbl, SMALL(IF(COUNTIF($S2, $B$2:$B$9)*COUNTIF($R2, $D$2:$D$9), 
    ROW(tbl)-MIN(ROW(tbl))+1), U$1), COLUMN($D$1))}
    

    This will copy down and across.

    0 讨论(0)
提交回复
热议问题