Adding st, nd, rd, th to ranked values

masud8956

Board Regular
Joined
Oct 22, 2016
Messages
163
Office Version
  1. 2016
  2. 2011
  3. 2007
Platform
  1. Windows
Hi everyone!

I am using the following formula to rank 20 rows of entries at S5:S24.

Code:
=IF(ISNUMBER(R5),1+COUNTIF($R$5:$R$24,">"&R5),"")

What modification do I need to get the texts st, nd, rd, th as ordinal suffix?

Regards
 

Excel Facts

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.
It's not pretty, but it will work.

Code:
=IF(ISNUMBER(R5),1+COUNTIF($R$5:$R$24,">"&R5),"")&IF(AND(MOD(ABS(IF(ISNUMBER(R5),1+COUNTIF($R$5:$R$24,">"&R5),"")),100)>10,MOD(ABS(IF(ISNUMBER(R5),1+COUNTIF($R$5:$R$24,">"&R5),"")),100)<14),"th",CHOOSE(MOD(ABS(IF(ISNUMBER(R5),1+COUNTIF($R$5:$R$24,">"&R5),"")),10)+1,"th","st","nd","rd","th","th","th","th","th","th"))

Should work to convert any cardinal number into an ordinal number. Note that these values will be stored as text and not as numbers anymore.
Alternatively, you could add the following into column T
Code:
=IF(AND(MOD(ABS(S5),100)>10,MOD(ABS(S5),100)<14),"th",CHOOSE(MOD(ABS(S5),10)+1,"th","st","nd","rd","th","th","th","th","th","th"))

This would leave the cardinal number in column S and add the ordinal suffix to column T which would aesthetically LOOK similar, but would allow the value in column S to be used as an actual number.
 
Last edited:
Upvote 0
Excellent solution @BiocideJ! Thanks a lot!

Using the 1st solution as value turning into text does not affect my purposes.

Just another little one, is there a way that I could get the ordinal suffixes as superscript automatically?
 
Upvote 0
It's not pretty, but it will work.

Code:
=IF(ISNUMBER(R5),1+COUNTIF($R$5:$R$24,">"&R5),"")&IF(AND(MOD(ABS(IF(ISNUMBER(R5),1+COUNTIF($R$5:$R$24,">"&R5),"")),100)>10,MOD(ABS(IF(ISNUMBER(R5),1+COUNTIF($R$5:$R$24,">"&R5),"")),100)<14),"th",CHOOSE(MOD(ABS(IF(ISNUMBER(R5),1+COUNTIF($R$5:$R$24,">"&R5),"")),10)+1,"th","st","nd","rd","th","th","th","th","th","th"))
A little prettier...

=A1&MID("thstndrdth",MIN(9,2*RIGHT(A1)*(MOD(A1-11,100)>2)+1),2)



Just another little one, is there a way that I could get the ordinal suffixes as superscript automatically?
That would require VBA event code. Can you make use of a VBA solution?
 
Last edited:
Upvote 0
The following two sub-routines, placed in the relevant worksheet’s code module, together with the third sub-routine and the accompanying function in a standard module, will automatically apply ordinal formatting to values entered into a range named "Ordinal":
Code:
Private Sub Worksheet_Calculate()
Ordinals ActiveSheet.Range("Ordinal")
End Sub

Private Sub Worksheet_Change(ByVal Target As Range)
Ordinals Target
End Sub
Note: you don’t need the code in red unless you also use the ‘OrdVal’ UDF described later.
Code:
[COLOR="#FF0000"]Public NmRng As String[/COLOR]
Sub Ordinals(ByVal Target As Range)
Dim oCell As Range
[COLOR="#FF0000"]If NmRng <> "" Then _
  ActiveWorkbook.Names.Add Name:="Ordinal", _
  RefersTo:=ActiveWorkbook.Names.Item("Ordinal") & "," & NmRng[/COLOR]
If Intersect(Target, ActiveSheet.Range("Ordinal")) Is Nothing Then Exit Sub
On Error Resume Next
For Each oCell In Target
  If IsNumeric(oCell.Value) Then oCell.NumberFormat = OrdFmt(oCell.Value)
Next
End Sub

Function OrdFmt(ByVal Num As Long) As String
Dim Cell As Range
If IsNumeric(Cell.Value) Then
  OrdFmt = "#""" & Mid$("thstndrdthththththth", 1 - 2 * _
    ((Cell.Value) Mod 10) * (Abs((Cell.Value) Mod 100 - 12) > 1), 2) & """"
End If
End Function
One limitation of User-Defined Functions (UDFs) is that they can only change the value displayed in the cell that contains the formula. They cannot change any other aspect of the workbook. This means you can’t use the UDF to change the cell’s number format or add it to the named range. They can, however, set a variable's properties, and here's where the workaround takes effect. By setting the ‘NmRng’ variable and incorporating the code in red, above, you can have a UDF that indirectly adds the target cell's address to the named range and exploit the 'Worksheet_Change' and 'Worksheet_Calculate' events to apply ordinal number formatting to a cell with a formula like:
=OrdVal(A1):
Code:
Function OrdVal(ByVal Num As Long) As Long
Application.Volatile
NmRng = ActiveSheet.Name & "!" & Selection.Address
If InStr(ActiveWorkbook.Names.Item("Ordinal"), NmRng) <> 0 Then NmRng = ""
OrdVal = Num
End Function
Alternatively, the UDF below can use a formula like;
=Ordinal (A1); or
=Ordinal(37),
for any cell on any worksheet to which the event-driven code is attached.
Code:
Function Ordinal(ByVal Num As Long) As Long
Ordinal = Num
End Function

Private Sub Worksheet_Change(ByVal Target As Range)
Dim Cell As Range
On Error Resume Next
For Each Cell In ActiveSheet.UsedRange
  If UCase(Cell.Formula) Like "=ORDINAL(?*)" Then
    Cell.NumberFormat = "#,#""" & Mid$("thstndrdthththththth", 1 - 2 * _
    ((Cell.Value) Mod 10) * (Abs((Cell.Value) Mod 100 - 12) > 1), 2) & """"
  End If
Next Cell
End Sub
The underlying values remain available for use as numbers in other formulae.
Of course, if only a limited range of cells in a large workbook can have this kind of formula, it would be wise to narrow the event's target range to just that. The above approach also allows a mix of ordinal and ordinary values to coexist in the target range.
Note: Decimal value ordinals are based on the nearest integer. As well, when driven by the Ordinal Function, the values are converted to strings.
 
Upvote 0
.. turning into text does not affect my purposes.

.. is there a way that I could get the ordinal suffixes as superscript automatically?
Combining Rick's formula from post 4 with some vba, you could try this Worksheet_Change code on a copy of your workbook.
To implement ..
1. Right click the sheet name tab and choose "View Code".
2. Copy and Paste the code below into the main right hand pane that opens at step 1.
3. Close the Visual Basic window & test.


Code:
Private Sub Worksheet_Change(ByVal Target As Range)
  Dim c As Range, rData As Range
  
  Set rData = Range("R5:R24")
  If Not Intersect(Target, rData) Is Nothing Then
    rData.Offset(, 1).Formula = Replace(Replace("=IF(ISNUMBER(^),1+COUNTIF(#,"">""&^),"""")", "#", rData.Address), "^", rData.Cells(1).Address(0, 0))
    For Each c In rData.Offset(, 1)
      If Len(c.Value) > 0 Then
        c.Value = c.Value & Evaluate("MID(""thstndrdth"",MIN(9,2*RIGHT(" & c.Value & ")*(MOD(" & c.Value & "-11,100)>2)+1),2)")
        c.Characters(Len(c.Value) - 1, 2).Font.Superscript = True
      End If
    Next c
  End If
End Sub
 
Upvote 0
is there a way that I could get the ordinal suffixes as superscript automatically?

You could also make use of the unicode superscript characters - using Rick's formula, if you copy this directly from the forum the ordinals should be superscript.

=A1&MID("ᵗʰˢᵗⁿᵈʳᵈᵗʰ",MIN(9,2*RIGHT(A1)*(MOD(A1-11,100)>2)+1),2)
 
Upvote 0
You could also make use of the unicode superscript characters ..
:beerchug:

Good thinking. So using this idea, which includes Rick's of course, and the OP's original formula, it could all be done at once.

Excel Workbook
RS
5516??
6613??
7258??
81411??
9Text
10613??
11#N/A
12453??
13324??
14812??
1523.2510??
16901??
17306??
18
19
20277??
21324??
22249??
23613??
24702??
Ordinals
 
Upvote 0
Wonderful solutions by all of you!

Each of them worked for me.

Cannot thank you experts enough!!
 
Upvote 0

Forum statistics

Threads
1,223,228
Messages
6,170,875
Members
452,363
Latest member
merico17

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