+ Reply to Thread
Results 1 to 11 of 11

Need to select earliest five rows matching highest value, and next highest if not enough

  1. #1
    Valued Forum Contributor
    Join Date
    01-16-2012
    Location
    England
    MS-Off Ver
    MS 365
    Posts
    1,397

    Need to select earliest five rows matching highest value, and next highest if not enough

    Hopefully the attachment will make everything clear.

    Need to select five entries that were the "first to score" values in a range.

    As shown on the attachment, it is straightforward if five or more entries achieve the Highest Score, Code needs to select the first five in the bloc, and copy and paste to a different location.

    But if there are less than five, code should copy and paste however many there are, then find the bloc with the Second Highest scorers.

    If there are more than the balance needed, copy however many are required, starting with the first match, and copy and paste them.

    If there are less than the balance, copy and paste what there is, and look for Third Highest number. Start with the first match, and copy and paste them.

    Initial thought was to use a formula in Col C to establish how many entries there are for each value, (=COUNTIF(B$1:B$21,B2) copied down),

    Starting at row 21 and working upward, the Code could then use that as the "offset", to measure whether each value has more than the number of rows required?
    And if there are more than five rows, it copies from the Offset cell down to the Offset,offset(5,0)

    Please Login or Register  to view this content.
    But the "options" I would have to build in suggest I am missing something much simpler.

    Any suggestions and pokinters received gratefully as ever.

    Ochimus
    Attached Files Attached Files

  2. #2
    Forum Expert
    Join Date
    06-12-2012
    Location
    Ridgefield Park, New Jersey
    MS-Off Ver
    Excel 2003,2007,2010
    Posts
    10,241

    Re: Need to select earliest five rows matching highest value, and next highest if not enou

    In your first sample in your spreadsheet, shouldn't Employee 5 be included?

  3. #3
    Valued Forum Contributor
    Join Date
    01-16-2012
    Location
    England
    MS-Off Ver
    MS 365
    Posts
    1,397

    Re: Need to select earliest five rows matching highest value, and next highest if not enou

    Quite right, John, simplhy missed that!

    But hopefully everyone can still understand the concept?

    Not often I'm stumped completely at the outset, but I seemed to be looking at an infinite series of "IF THENs" even if I got the Code to sort out the relevant ranges, so I'm certain someone will have a "better mousetrap" to solve it..

    Ochimus

  4. #4
    Forum Expert
    Join Date
    06-12-2012
    Location
    Ridgefield Park, New Jersey
    MS-Off Ver
    Excel 2003,2007,2010
    Posts
    10,241

    Re: Need to select earliest five rows matching highest value, and next highest if not enou

    Maybe:

    Please Login or Register  to view this content.
    Last edited by JOHN H. DAVIS; 05-04-2016 at 03:06 PM. Reason: Modification

  5. #5
    Forum Expert
    Join Date
    05-20-2015
    Location
    Chicago, Illinois
    MS-Off Ver
    2016
    Posts
    2,103

    Re: Need to select earliest five rows matching highest value, and next highest if not enou

    I gave it a shot with a formula. Try the formula below in I23, confirm it with Ctrl + Shift + Enter instead of Enter, then fill down through I27. You can modify the column letters for your other datasets. See if it does what you're after? It's returning the right employees for me, even as I alter the data.

    =INDEX($I$1:$I$21,1/RIGHT(LARGE($J$2:$J$21+1/ROW($J$2:$J$21),ROW()-22),LEN(LARGE($J$2:$J$21+1/ROW($J$2:$J$21),ROW()-22))-1))

    EDIT: If your top five aren't always going to start in row 23, then the version below may be more manageable:

    =INDEX($I$1:$I$21,1/RIGHT(LARGE($J$2:$J$21+1/ROW($J$2:$J$21),ROW(1:1)),LEN(LARGE($J$2:$J$21+1/ROW($J$2:$J$21),ROW(1:1)))-1))
    Last edited by CAntosh; 05-04-2016 at 01:22 PM.

  6. #6
    Valued Forum Contributor
    Join Date
    01-16-2012
    Location
    England
    MS-Off Ver
    MS 365
    Posts
    1,397

    Re: Need to select earliest five rows matching highest value, and next highest if not enou

    cantosh,

    Ingenious! I shall now lose ten nights' sleep working out how it does it, but it's exactly what I needed.

    John, appreciate the approach as ever, but in this case having a formula solve it is ideal.

    Ochimus

  7. #7
    Forum Expert
    Join Date
    06-12-2012
    Location
    Ridgefield Park, New Jersey
    MS-Off Ver
    Excel 2003,2007,2010
    Posts
    10,241

    Re: Need to select earliest five rows matching highest value, and next highest if not enou

    You're welcome. Glad too hear you have a solution that works for you.

  8. #8
    Forum Expert
    Join Date
    05-20-2015
    Location
    Chicago, Illinois
    MS-Off Ver
    2016
    Posts
    2,103

    Re: Need to select earliest five rows matching highest value, and next highest if not enou

    I'm glad I could help. In case you need to make modifications, the rough breakdown from the inside out is as follows:

    Whole Thing: =INDEX($I$1:$I$21,1/RIGHT(LARGE($J$2:$J$21+1/ROW($J$2:$J$21),ROW(1:1)),LEN(LARGE($J$2:$J$21+1/ROW($J$2:$J$21),ROW(1:1)))-1))

    $J$2:$J$21+1/ROW($J$2:$J$21) takes the values in J and adds 1/ROW. This effectively breaks ties, giving primacy to the earlier entries when there's a tie.

    LARGE($J$2:$J$21+1/ROW($J$2:$J$21),ROW(1:1)) The LARGE function gives us our top rank and paves the way to fill down 5 rows for the top 5. This gives us the top ranked J+1/ROW values, which is what you're after, but now we have to work back out of the ugly J+1/ROW decimals to more useful values that can be used to locate the associated employees.

    RIGHT(LARGE($J$2:$J$21+1/ROW($J$2:$J$21),ROW(1:1)),LEN(LARGE($J$2:$J$21+1/ROW($J$2:$J$21),ROW(1:1)))-1) This looks busy, but all it does is remove the first digit of the value thus far, leaving us with 1/ROW. It dawns on me now that you could shorten this by simply subtracting J2:J21 instead of using RIGHT.

    1/RIGHT(LARGE($J$2:$J$21+1/ROW($J$2:$J$21),ROW(1:1)),LEN(LARGE($J$2:$J$21+1/ROW($J$2:$J$21),ROW(1:1)))-1) Brings us back to the Row number: 1/(1/ROW) = ROW. This portion of the formula alone returns the Row number of the (1st) largest value. The final step then just uses INDEX on column I to return the corresponding employee. It looks like a jumbled mess all together, but in actuality the formula just builds a tie-breaker to identify the desired values, then unravels it.

  9. #9
    Valued Forum Contributor
    Join Date
    01-16-2012
    Location
    England
    MS-Off Ver
    MS 365
    Posts
    1,397

    Re: Need to select earliest five rows matching highest value, and next highest if not enou

    cantosh,

    Attached should make the new question clear.

    I23 - I27 shows the names from Col I matching the criteria in Col L perfectly (when the range is sorted by Col L).

    But Col M has a separate sequence of numbers representing the Total activity from Day 1

    As shown below, I tried to adapt your formula to match the Names in Col I to these new value:

    {=INDEX($I$1:$I$21,1/RIGHT(LARGE($M$2:$M$21+1/ROW($M$2:$M$21),ROW()-22),LEN(LARGE($M$2:$M$21+1/ROW($M$2:$M$21),ROW()-22))-1))}

    As you can see in K23 - K27 it says "0" in each case, and I'm not sure why it is not working?

    Ochimus
    Attached Files Attached Files

  10. #10
    Forum Expert
    Join Date
    05-20-2015
    Location
    Chicago, Illinois
    MS-Off Ver
    2016
    Posts
    2,103

    Re: Need to select earliest five rows matching highest value, and next highest if not enou

    Ah, I understand now. My original wasn't as versatile as it should have been, so it tripped over the multi-digit values. Try this one (it's still an array formula, so Ctrl + Shift + Enter):

    =INDEX($I$1:$I$21,1/(LARGE($M$2:$M$21+1/ROW($M$2:$M$21),ROW(1:1))-INT(LARGE($M$2:$M$21+1/ROW($M$2:$M$21),ROW(1:1)))))

  11. #11
    Valued Forum Contributor
    Join Date
    01-16-2012
    Location
    England
    MS-Off Ver
    MS 365
    Posts
    1,397

    Re: Need to select earliest five rows matching highest value, and next highest if not enou

    canmtosh,

    Many thsnks, the tweak works perfectly.Another perpetual manual chore bites the dust!

    Ochimus

+ 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] Max formula to return total of highest, second highest and third highest value
    By JonWilf in forum Excel Formulas & Functions
    Replies: 5
    Last Post: 04-06-2016, 08:20 AM
  2. Find highest value in table and return earliest occurence (name)
    By potatoman in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 09-10-2014, 02:44 PM
  3. Macro to select rows with respect to highest value in column
    By gokzee in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 12-05-2012, 09:41 PM
  4. 2 columns of data matching highest with highest
    By ronaldchristie in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 08-03-2012, 07:57 AM
  5. VBA code to find highest and 2nd highest number based in criteria
    By Michael007 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 10-25-2011, 08:38 AM
  6. Replies: 2
    Last Post: 09-19-2008, 10:22 AM
  7. Replies: 3
    Last Post: 08-10-2006, 11:40 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