星期二, 十一月 29, 2011

Finding the 2nd, 3rd, 4th … MATCH()

From Daily Does of Excel
Finding the 2nd, 3rd, 4th … MATCH():

Excel’s MATCH() function will return the relative location of the first occurrence that satisfies the match criterion (the lookup_value) within the specified array (the lookup_array). Most often, this is what you want. For instance, if you have duplicate entries, returning the first match is no different than returning the last match. However, if you have unique entries with the duplicated criterion, how do you return all the unique values?


For example, you have a list of the 122 major professional sports teams (32 NFL, and 30 MLB, NBA, and NHL) sorted alphabetically in Column D. In Column E you have code that returns the length of the team name, i.e. =LEN(D1). Your data might look like this:




























































DE
1Anaheim Ducks13
2Arizona Cardinals17
3Arizona Diamondbacks20
4Atlanta Braves14
5Atlanta Falcons15
6Atlanta Hawks13
7Baltimore Orioles17
8Baltimore Ravens16
9Boston Bruins13
10Boston Celtics14

For no good reason we want to create a descending sort on name length. You can do it from the Sort selection, but we also want to keep the alpha-sort. In F1 you put =LARGE($E$1:$E$122,ROW()) and fill down. The ROW() function will cause the return of the first largest, then the second largest etc. Then you want to know how far down Column E those descending numbers fall, so that you can Index the name list that amount. In G1 put the code =MATCH(F1,$E$1:$E$122,0) and fill down. You’ll see from the data that the longest name is 29 letters, and it’s in ROW(52).


















































































DEFG
1Anaheim Ducks132952
2Arizona Cardinals172230
3Arizona Diamondbacks202230
4Atlanta Braves142230
5Atlanta Falcons152142
6Atlanta Hawks132142
7Baltimore Orioles172142
8Baltimore Ravens16203
9Boston Bruins13203
10Boston Celtics14203

You can begin to see the problem: There are three teams with a 22-letter name, three with a 21-letter name, and at least 3 (in fact 13) with a 20-letter name. If in H1 we put =INDEX(D:D,G1) and filldown, we get:





























































































DEFGH
1Anaheim Ducks132952Los Angeles Angels of Anaheim
2Arizona Cardinals172230Columbus Blue Jackets
3Arizona Diamondbacks202230Columbus Blue Jackets
4Atlanta Braves142230Columbus Blue Jackets
5Atlanta Falcons152142Golden State Warriors
6Atlanta Hawks132142Golden State Warriors
7Baltimore Orioles172142Golden State Warriors
8Baltimore Ravens16203Arizona Diamondbacks
9Boston Bruins13203Arizona Diamondbacks
10Boston Celtics14203Arizona Diamondbacks

This is exactly what we wanted to avoid. So, using Col(H) now as a helper column, in H1 put =G1. In H2 put



  • =IF(G2<>G1,G2,H1+MATCH(F2,INDIRECT(“E”&(H1+1)&”:E122″),0))


and fill down. If the value did not repeat, just copy it over; and if it didn’t, we’re making a “sliding range” that starts one row after the start of a repeated pair, ends at the end of the data, and “slides” down Column E. The sliding range is made by the INDIRECT() function. It builds the range one row down (H1+1) from the first of a repeating match. We add H1 to that result, and we have our index down Column D. This may be better seen in H3, adjacent to G3, where the first repeat starts.



  • =IF(G3<>G2,G3,H2+MATCH(F3,INDIRECT(“E”&(H2+1)&”:E122″),0))


The IF() is false. We are looking for the relative location of the lookup_value (22) in a new lookup_range (E31:E122). It’s 33 rows in. We add the original 30 to that, and the index is now 63. A similar thing happens in H4. H5 is G5 copied over. In Column I, I1 =INDEX(D:D,H1), filled down. The resulting table:








































































































DEFGHI
1Anaheim Ducks13295252Los Angeles Angels of Anaheim
2Arizona Cardinals17223030Columbus Blue Jackets
3Arizona Diamondbacks20223063Minnesota Timberwolves
4Atlanta Braves14223095Portland Trail Blazers
5Atlanta Falcons15214242Golden State Warriors
6Atlanta Hawks13214283Oklahoma City Thunder
7Baltimore Orioles17214289Philadelphia Phillies
8Baltimore Ravens162033Arizona Diamondbacks
9Boston Bruins1320349Jacksonville Jaguars
10Boston Celtics1420353Los Angeles Clippers

The names are now sorted by length, by alphabet. INDIRECT() is volatile, so there is a penalty for this. For extra credit, and no peeking, what’s the shortest name?


…mrt

©¿©¬

没有评论: