+ Reply to Thread
Results 1 to 9 of 9

find the nth occurrence from the bottom of a column

  1. #1
    Registered User
    Join Date
    10-25-2013
    Location
    canada
    MS-Off Ver
    Excel 2007
    Posts
    79

    find the nth occurrence from the bottom of a column

    Is there a way to find the nth occurrence of a value in a column using vba code?

    so a little info on what im looking for.

    im working on a hockey game simulator. the columns below represent which team scored and which player scored the goal. what i would like to achieve is figure out which player scored the game winning goal ( in the example below it would be playerD )

    teamA playerA
    teamA playerB
    teamB playerC
    teamA playerD
    teamB playerE
    teamA playerF

    i have been playing with using the difference in goals between the teams as an integer and then working my way up the column to find the winning goal (if possible!!) but cant quite figure that out

    keep in mind that the teams, number of goals, and order of goals will be dynamic and always different. i foresee a possible issue with games where one team gets no goals, but hopefully thats not the case.

    i will keep trying to figure this one out on my own, but any help would be greatly appreciated

    thanks

  2. #2
    Forum Guru samba_ravi's Avatar
    Join Date
    07-26-2011
    Location
    Hyderabad, India
    MS-Off Ver
    Excel 2021
    Posts
    8,914

    Re: find the nth occurrence from the bottom of a column

    In this case what is the result as per your requirement
    Samba

    Say thanks to those who have helped you by clicking Add Reputation star.

  3. #3
    Registered User
    Join Date
    10-25-2013
    Location
    canada
    MS-Off Ver
    Excel 2007
    Posts
    79

    Re: find the nth occurrence from the bottom of a column

    in the example i gave i would want the 2nd from the bottom occurrence of teamA

  4. #4
    Administrator FDibbins's Avatar
    Join Date
    12-29-2011
    Location
    Duncansville, PA USA
    MS-Off Ver
    Excel 7/10/13/16/365 (PC ver 2310)
    Posts
    52,926

    Re: find the nth occurrence from the bottom of a column

    I have something that will do what you want, but it involves the use of helper columns (which you can hide if you want.

    Assuming your data looks like this...

    A
    B
    C
    D
    E
    1
    TeamA TeamB
    2
    teamA playerA
    1
    0
    playerD
    3
    teamA playerB
    2
    0
    4
    teamB playerC
    2
    1
    5
    teamA playerD
    3
    1
    6
    teamB playerE
    3
    2
    7
    teamA playerF
    4
    2


    E2=INDEX($B$2:$B$7,MATCH(LARGE($D$2:$D$7,1)+1,$C$2:$C$7,0))
    1. Use code tags for VBA. [code] Your Code [/code] (or use the # button)
    2. If your question is resolved, mark it SOLVED using the thread tools
    3. Click on the star if you think someone helped you

    Regards
    Ford

  5. #5
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: find the nth occurrence from the bottom of a column

    Another way:

    Row\Col
    A
    B
    C
    D
    1
    Team
    Player
    2
    A
    Abel -- C2: =IF(COUNTIF(A$1:A2, A2) = COUNTIF($A$2:$A$7, "<>" & A2) + 1, "winner", "--")
    3
    A
    Bill --
    4
    B
    Cain --
    5
    A
    Drew winner
    6
    B
    Eric --
    7
    A
    Fred --
    Entia non sunt multiplicanda sine necessitate

  6. #6
    Administrator FDibbins's Avatar
    Join Date
    12-29-2011
    Location
    Duncansville, PA USA
    MS-Off Ver
    Excel 7/10/13/16/365 (PC ver 2310)
    Posts
    52,926

    Re: find the nth occurrence from the bottom of a column

    I knew there had to be a better way shg

  7. #7
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: find the nth occurrence from the bottom of a column

    I tried using subtotal/offset to create a single-cell formula, but didn't get there.

    Edit: Oops, only did if for one side:

    Row\Col
    A
    B
    C
    D
    1
    Team
    Player
    2
    A
    Abel -- C2: =IF((COUNTIF($A$2:$A$11, A2) > COUNTIF($A$2:$A$11, "<>" & A2)) * (COUNTIF(A$1:A2, A2) = COUNTIF($A$2:$A$11, "<>" & A2) + 1), "winner", "--")
    3
    B
    Bill --
    4
    B
    Cain --
    5
    A
    Drew --
    6
    B
    Eric --
    7
    A
    Fred --
    8
    B
    Gary --
    9
    A
    Hank --
    10
    B
    Ivan winner
    11
    B
    John --
    12
    Last edited by shg; 10-18-2014 at 05:20 PM.

  8. #8
    Administrator FDibbins's Avatar
    Join Date
    12-29-2011
    Location
    Duncansville, PA USA
    MS-Off Ver
    Excel 7/10/13/16/365 (PC ver 2310)
    Posts
    52,926

    Re: find the nth occurrence from the bottom of a column

    I was fiddling around with this (wasnt finished)...
    =IF(COUNTIF($A$2:A2,C$1)-1=COUNTIF($A$2:A2,D$1),IF(COUNTIF($A$2:$A2,C$1)-D2>1,B2,""))
    but yours is cleaner

  9. #9
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: find the nth occurrence from the bottom of a column

    Reckon I could eliminate some of the duplicate calculations ...

    Row\Col
    A
    B
    C
    D
    1
    Team
    Score
    2
    A
    6
    B2: =COUNTIF($A$5:$A$15, A2)
    3
    B
    4
    B3: =COUNTIF($A$5:$A$15, A3)
    4
    Winner
    A
    B4: =IF(B2>B3, A2, IF(B3>B2, A3, "draw"))
    5
    Team
    Player
    6
    A
    Abel -- C6: =IF((A6 = $B$4) * (COUNTIF(A$5:A6, A6) = MIN($B$2:$B$3) + 1), "winner", "--")
    7
    B
    Bill --
    8
    B
    Cain --
    9
    B
    Drew --
    10
    A
    Eric --
    11
    A
    Fred --
    12
    A
    Gary --
    13
    B
    Hank --
    14
    A
    Ivan winner
    15
    A
    John --
    Last edited by shg; 10-18-2014 at 06:51 PM.

+ 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. Replies: 3
    Last Post: 09-15-2014, 07:56 PM
  2. [SOLVED] Find the first occurrence of any Date in a column
    By maw230 in forum Excel Formulas & Functions
    Replies: 14
    Last Post: 03-14-2014, 02:43 PM
  3. [SOLVED] Find last occurrence of text in a column and return value in next column
    By LindaLu in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 02-06-2014, 09:45 AM
  4. Find bottom most value in a column range.
    By brc1981 in forum Excel Formulas & Functions
    Replies: 11
    Last Post: 01-08-2014, 05:49 PM
  5. VB Code to find 1st Occurrence of text in column A
    By rizmomin in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 12-25-2013, 09:46 AM

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