How to rank text uniquely in excel
Question: How do I rank text cell values uniquely? If text values were sorted alpabetically from A to Z, the first text value would rank 1 and so on... Duplicate text values rank uniquely.
Answer:
Array formula in B2:
Copy cell B2 and paste down as far as needed.
Named ranges
List (A2:A13)
How does this formula work?
Let us start with cell B2.
=COUNTIF(List, "<"&A2)+SUM(IF(A2=$A$2:A2, 1, 0))
Step 1 - Calculate rank
=COUNTIF(List, "<"&A2)
becomes
=COUNTIF({DD, AA, AA, BB, AA, DD, EE, AA, BB, EE, CC, EE}, "<"&DD)
and returns 7. 7 values are sorted before DD, as if they were sorted from A to Z.
Step 2 - Count current value and prior instances of current value
Keep in mind, countif doesn´t rank uniquely if duplicate values are found.
SUM(IF(A2=$A$2:A2, 1, 0))
becomes
SUM(IF("DD"="DD", 1, 0))
becomes
SUM(1) and returns 1
Step 3 - Add values
Step 1 returned 7 and step 2 returned 1. 7 + 1 equals 8.
Final comments
Select B3 and use "evaluate formula" to see how the next cell calculates rank. Click "Evaluate" button and excel calculates cell formula step by step.
Download excel example file.
text-values-uniquely-ranked.xls
(Excel 97-2003 Workbook *.xls)
Functions in this article:
IF(logical_test;[value_if:true];[value_if_false])
Checks whether a condition is met, and returns one value if TRUE, and another value if FALSE
SUM(number1,[number2],)
Adds all the numbers in a range of cells
COUNTIF(range,criteria)
Counts the number of cells within a range that meet the given condition







November 3rd, 2009 at 6:01 am
The formula works exactly as advertised and I have implemented it with no problems. However, even when using the "Evaluate Formula" feature to walk through the steps Excel uses to generate the output, I still don't understand how this formula works.
For instance, using the example file you provided for download, when I evaluate Cell B2 (which correctly ends up as 10), the final line before producing the output is:
MATCH(7.00000763673575,{6.00000763673575},0)
How is the MATCH function returning a 10 out of this? I sorta think I understand all the steps in the evaluation up to this point, but the money line eludes me. Would it be possible to break this formula down and explain the logic further.
If not, thanks for the formula anyway. I know how to use it even if I don't understand it.
November 3rd, 2009 at 6:03 am
And by Cell B2 I mean Cell B8, oops
November 3rd, 2009 at 11:33 am
Thank you for your comment!
Really good question, I have no answer why MATCH(7.00000763673575,{6.00000763673575},0) works.
I have changed the array formula in this post to a smaller, easier array formula. I also updated the attached excel file.
November 3rd, 2009 at 6:29 pm
Oscar,
Thank you, the new formula works just as well and is obviously simpler.
But now you've piqued my interest. Where did the original come from? It seems like a rather complicated formula to have been derived by trial & error (although possible I guess), so somebody must have created it on purpose.
Thanks again.
November 3rd, 2009 at 7:35 pm
If you use this formula, then you can enter it normally (that is, you do *not* have to use Ctrl+Shift+Enter, just Enter will work)...
=COUNTIF(A$2:A$20,"<"&A2)+COUNTIF($A$2:A2,A2)
November 3rd, 2009 at 7:37 pm
If you use this formula, then you can enter it normally (that is, you do *not* have to use Ctrl+Shift+Enter, just Enter will work)...
=COUNTIF(List,"<"&A2)+COUNTIF($A$2:A2,A2)
November 3rd, 2009 at 8:28 pm
Matt Gahlinger,
I created the original formula by trial and error. It is a weird formula that should not work, I think. But it it worked and then I forgot about it.
Rick Rothstein (MVP - Excel),
Thanks!
November 4th, 2009 at 12:58 am
Guys,
All the formulas work as far as I can tell. Thanks for the multiple solutions.
April 4th, 2011 at 10:11 am
Will the formula work if "List" (a named range) is substituted by A$2:A$13 instead?
I need to rank several text columns and would like to copy & paste the formulas & not have to define a name for each list.
April 4th, 2011 at 1:03 pm
Steve,
Yes, it will work.
April 4th, 2011 at 2:44 pm
Tried it & it worked perfectly. Thanks.
November 28th, 2011 at 11:03 pm
Tried it, and copied colums a and b to my excel, and it worked. But I could not copy and paste just the formula in my own excel, nor could I rewrite it without losing the unique part. I use Excel 2007
November 30th, 2011 at 4:08 pm
Brendon,
=COUNTIF(List, "<"&A2)+SUM(IF(A2=$A$2:A2, 1, 0))
You need to adjust cell references in both named range List and in formula.
February 7th, 2012 at 1:47 pm
My real data returned many duplicated rankings with the original formula. I used this to give unique rankings for each entry:
=COUNTIF(List, "<"&A2)+COUNTIF($A$2:A2,A2)
Just noticed that's effectively what "Rick Rothstein (MVP - Excel)" posted, so that makes two of us.