A family of Microsoft spreadsheet software with tools for analyzing, charting, and communicating data.
I'm glad to hear a humble Word MVP can contribute something useful for you Excel geeks!!!
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
Hello,
I am wondering if anyone knows (or knows of any resources that describe) how to create an ordinal number format in MS Excel.
I am well aware of how to reformat numbers as text to create the ordinal number effect, but what I would like to do is have the numberdisplay as an ordinal number (e.g., "1st", "2nd", "3rd", etc.) while still retaining its numeric value (1, 2, 3, etc.).
The reason for this request is to be able to perform numeric functions (e.g., MIN, MAX, AVERAGE, etc.) on an array of ordinal numbers.
I have a spreadsheet that lists various athletes' scores and the ranking for each score. I would like to be able to easily determine, for example, each athlete's best ranking by using a MIN() function on the list of ordinal numbers. Currently the only way I can think to do this is by having the rankings as regular cardinal numbers.
I suppose it would be possible to have an adjacent, hidden column with the numeric value of each ranking, but I thought it might be more elegant if there was a way to change the format of the number without changing its value.
Any help is much appreciated!
Regards,
Craig
A family of Microsoft spreadsheet software with tools for analyzing, charting, and communicating data.
Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.
I'm glad to hear a humble Word MVP can contribute something useful for you Excel geeks!!!
By the way, it was early in the wee hours of the morning (just before I went to bed) when I posted my message and forgot to mention how much I liked your hybrid "UDF/Event Code" solution... that is an excellent method to circumvent the UDF restriction of not being able to take any actions other than returning a value to its host cell... I realy liked it!
Hi Rick,
Good point. I'll edit the original.
I think you should change this line of code...
If Cell.Formula Like "=Ordinal(?*)" Then
to this...
If UCase(Cell.Formula) Like "=ORDINAL(?*)" Then
so that the user doesn't have to type the function name with the exact same casing you specified in your If..Then test.
The following UDF, when combined with a modification of Rick Rothstein's event code (supplied), can be used to express the value in any cell as an ordinal:
Function Ordinal(ByVal Num As Long) As Long
Ordinal = Num
End Function
Private Sub Worksheet_Change(ByVal Target As Range)
Dim Cell As Range
For Each Cell In Target
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
End Sub
To use it, simply input a formula into a cell as, for example:
=Ordinal(A1) or =Ordinal(37)
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. Unlike Rick's event-driven macro, the above approach allows a mix of ordinal and ordinary values to coexist in the target range.