1. ## Function ConvertToOrdinal

As a followup to my question about converting cardinal numbers to ordinal numbers (<A target="_blank" HREF=http://www.wopr.com/cgi-bin/w3t/showflat.pl?Cat=&Board=vb&Number=39092&page=0&view =expanded&sb=5>Cardinal Numbers to Ordinal Numbers </A>), here is the code I came up with. Please feel free, as always, to suggest any ways to optimize the code.

<pre>Public Function ConvertToOrdinal( _
ByRef rstrNumberToConvert As String _
) As String

'----------------------------------------
'Purpose: Given an cardinal number (i.e., 1, 2, 204, 1908)
' converts to an ordinal number (i.e., 1st, 2nd, 204th, 1908th)
'
'Process: The function uses ranges instead of selections to
' accomplish the purpose. This is because with ranges the
' screen does not "flash."
' Therefore, I set the range to the end of the document,
' add a field, which does, by the way, actually insert the
' field into the document, reset the range around the field,
' give the result of the field to the function, and delete
' the range (the field).
' Since it is done with ranges, the cursor never leaves
' its position and the screen does not flash to the bottom of
' the document.

Dim myRange As Range

Set myRange = activedocument.Content
myRange.Collapse Direction:=wdCollapseEnd

Type:=wdFieldEmpty, _
Text:="= " + rstrNumberToConvert + " * Ordinal", _
PreserveFormatting:=True

myRange.SetRange start:=myRange.start, End:=activedocument.Range.End - 1

ConvertToOrdinal = myRange.Fields(1).Result

myRange.Delete

End Function</pre>

2. ## Re: Function ConvertToOrdinal

Here is another more general approach, less dependant on Word. It is NOT proposed as an improvement or comment on your perfectly good piece of coding. <pre>Function OrdNumber(oNum As String) As String
Dim Ord As String, Sufx As String
Ord = Format(Right(oNum, 2), "00")
Sufx = "th"
If Left(Ord, 1) <> "1" Then
Select Case Right(Ord, 1)
Case "1"
Sufx = "st"
Case "2"
Sufx = "nd"
Case "3"
Sufx = "rd"
End Select
End If
OrdNumber = Format(Int(oNum), "#,##0") & Sufx
End Function</pre>

Perhaps you, or anyone else, have comments or improvements. This was tested only in Excel but should be usable elsewhere.

Andrew C

3. ## Re: Function ConvertToOrdinal

I do like that.

I don't like having muck around inside the document itself. Too much opportunity for something to go wrong and leave unknown residue in the document.

I will give it a look-see in Word.

Thanks.

4. ## Re: Function ConvertToOrdinal

Al, a variation on a theme, one for dates<pre>Function OrdDate(oDate As Date)
Dim oDay As String
oDay = Format(Day(oDate), "00")
Sfx = "th"
If Left(oDay, 1) <> "1" Then
Select Case Right(oDay, 1)
Case "1"
Sfx = "st"
Case "2"
Sfx = "nd"
Case "3"
Sfx = "rd"
End Select
End If
OrdDate = Val(oDay) & Sfx & " " & Format((oDate), "mmmm") & " " & Format((oDate), "YYYY")
End Function</pre>

this should return 16th May 2001 if passed today's date

Andrew

5. ## Re: Function ConvertToOrdinal

Slicker than slick. Just as the doctor ordered.

Thanks! <img src=/S/bravo.gif border=0 alt=bravo width=16 height=30>

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•