VBA or Excel Function to sort

birkato

New Member
Joined
Aug 15, 2013
Messages
8
Hi, I have a set of data. Lets say there are 10 people that answer and bunch of questions and based on the questions, there is a formula that calculates 10 separate "scores" that describe the person. So for each person there are 10 "scores" (say Score A through Score J) that range from 0 to 100.

What I need to do is to rank them as 1st, 2nd...10th and to identify the score type for each one. I currently am doing this with a formula using the LARGE function (no VBA). This works great but the only catch is that some of the scores are identical ties. Say for instance I have the following scores:
Score A = 100
Score B = 90
Score C = 80
Score D = 80

Score E = 70
Score F = 60
Score G = 50
Score H = 40
Score I = 30
Score J = 20

My LARGE function currently produce the following ranks....1st = Score A, 2nd Score B, 3rd = Score C, 4th = Score C, 5th = Score E....(it skips Score D). I can understand why as this function probably doesn't have any way of breaking a tie. I realize this functionality might require a VBA script. I am OK with VBA, but does anyone have any pointers on how to start? I was thinking I would read in all 10 scores for a user, set a DO UNTIL loop with an index to run 10 times, and cycle through finding the highest to the lowest. I just don't know how to exclude or deal with the case when there is a tie. How do I tell it to accept one as the first tied rank and then accept the second tied score as the other rank? Not looking for someone to code this for me, I just am looking for some tips to get started. Would it be easier to literally tell excel to write the scores along with the score types, then issue a command to re-sort the data and then read it back in? This would be simple to do by hand if there were a few users but I have thousands of users.
 

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!
Hi,

Before taking refuge to VBA;
try this:

Book1
ABC
1Person 1Score A = 1001
2Person 1Score B = 902
3Person 1Score C = 803
4Person 1Score D = 804
5Person 1Score E = 705
6Person 1Score F = 606
7Person 1Score G = 507
8Person 1Score H = 408
9Person 1Score I = 309
10Person 1Score J = 2010
11Person 2Score J = 2010
12Person 2Score G = 507
13Person 2Score H = 408
14Person 2Score C = 803
15Person 2Score D = 804
16Person 2Score F = 606
17Person 2Score I = 309
18Person 2Score E = 705
19Person 2Score A = 1001
20Person 2Score B = 902
Sheet1
Cell Formulas
RangeFormula
C1=COUNTIFS(B$1:B$20,"<"&B1,$A$1:$A$20,A1)+1
 
Upvote 0
Hi: Thanks for this suggestion and I see what the formula is trying to do. For me though, it still does not work when there is a tied score (mine is repeating the rank similar to the LARGE function does). See data below. Person 1 ranking is OK but Person 2 repeats rank #3. Person 3 is OK and shows that the scores don't have to be in any specific order for the formula to work. What am I doing wrong? Here is a copy/paste of the formula I am using in cell C2:
=COUNTIFS(B$2:B$31,">"&B2,$A$2:$A$31,A2)+1

PersonSCORERANK
Person 11001
Person 1902
Person 1803
Person 1704
Person 1605
Person 1506
Person 1407
Person 1308
Person 1209
Person 11010
Person 21001
Person 2902
Person 2803
Person 2803
Person 2605
Person 2506
Person 2407
Person 2308
Person 2209
Person 21010
Person 3951
Person 3607
Person 3706
Person 31010
Person 3159
Person 3168
Person 3852
Person 3843
Person 3834
Person 3825

<tbody>
</tbody><colgroup><col><col><col></colgroup>
 
Upvote 0
Hi,

You've applied the formula correct but your data has changed. You're trying to apply the formula which was meant for ranking alphanumeric values onto a series of numeric values. If it only was this simple....
If you want to sort the numeric values in ascending or descending order you'll need to apply some kind of RANK IF construction.
As it's Excel there are many solution for a RANK IF, this is one:

ABC
1PersonSCORERANK
2Person 11001
3Person 1902
4Person 1803
5Person 1704
6Person 1605
7Person 1506
8Person 1407
9Person 1308
10Person 1209
11Person 11010
12Person 21001
13Person 2902
14Person 2803
15Person 2804
16Person 2605
17Person 2506
18Person 2407
19Person 2308
20Person 2209
21Person 21010
22Person 3951
23Person 3607
24Person 3706
25Person 31010
26Person 3159
27Person 3168
28Person 3852
29Person 3843
30Person 3834
31Person 3825

<colgroup><col style="width: 25pxpx"><col><col><col></colgroup><thead>
</thead><tbody>
</tbody>
Sheet1

Worksheet Formulas
CellFormula
C2=COUNTIFS($A$2:$A$31,$A2,$B$2:$B$31,">"&$B2)+COUNTIFS($A$2:$A2,$A2,$B$2:$B2,$B2)

<thead>
</thead><tbody>
</tbody>

<tbody>
</tbody>

Hope this helps
 
Last edited:
Upvote 0
Another way to break ties is to add the row data (divided by 1000) as a tie breaker.


Unknown
ABCD
1Score A100Score J
2Score B90Score I
3Score C80Score H
4Score D80Score G
5Score E70Score F
6Score F60Score E
7Score G50Score C
8Score H40Score D
9Score I30Score B
10Score J20Score A
Sheet1
Cell Formulas
RangeFormula
D1{=INDEX($A$1:$A$10, 1000*MOD(SMALL($B$1:$B$10+ROW($B$1:$B$10)/1000,ROWS($1:1)),1), 1)}
D2{=INDEX($A$1:$A$10, 1000*MOD(SMALL($B$1:$B$10+ROW($B$1:$B$10)/1000,ROWS($1:2)),1), 1)}
D3{=INDEX($A$1:$A$10, 1000*MOD(SMALL($B$1:$B$10+ROW($B$1:$B$10)/1000,ROWS($1:3)),1), 1)}
D4{=INDEX($A$1:$A$10, 1000*MOD(SMALL($B$1:$B$10+ROW($B$1:$B$10)/1000,ROWS($1:4)),1), 1)}
D5{=INDEX($A$1:$A$10, 1000*MOD(SMALL($B$1:$B$10+ROW($B$1:$B$10)/1000,ROWS($1:5)),1), 1)}
D6{=INDEX($A$1:$A$10, 1000*MOD(SMALL($B$1:$B$10+ROW($B$1:$B$10)/1000,ROWS($1:6)),1), 1)}
D7{=INDEX($A$1:$A$10, 1000*MOD(SMALL($B$1:$B$10+ROW($B$1:$B$10)/1000,ROWS($1:7)),1), 1)}
D8{=INDEX($A$1:$A$10, 1000*MOD(SMALL($B$1:$B$10+ROW($B$1:$B$10)/1000,ROWS($1:8)),1), 1)}
D9{=INDEX($A$1:$A$10, 1000*MOD(SMALL($B$1:$B$10+ROW($B$1:$B$10)/1000,ROWS($1:9)),1), 1)}
D10{=INDEX($A$1:$A$10, 1000*MOD(SMALL($B$1:$B$10+ROW($B$1:$B$10)/1000,ROWS($1:10)),1), 1)}
Press CTRL+SHIFT+ENTER to enter array formulas.
 
Upvote 0

Forum statistics

Threads
1,215,459
Messages
6,124,944
Members
449,198
Latest member
MhammadishaqKhan

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top