I would like to display a date in the format 7th September 2006 in a cell. I
want to do this as a cell format so that I can refer to the cell and Excel
will recognise it as a date, not text.
Is there any way to show ordinal numbers in this way using cell formatting?
You would have to set the cell format using an event-triggered VBA macro.
The macro below should work reliably for dates using the 1900 date system that
are on or after 1 Mar 1900. If you are using the 1904 date system, or dates
before 1 Mar 1900, it will require some additional lines due in part to the
"built-in" bug in Excel which allows 29 Feb 1900 as a valid date. (I'm told
that was done for compatibility with Lotus).
To enter this macro, right click the sheet tab and select View Code. Paste the
macro below into the window that opens.
You will have to "Set" AOI to the range that you wish to have formatted in this
manner.
Let me know if this does what you want:
=============================================
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
Dim AOI As Range, c As Range
Dim Suffix As String
Dim dt As Date
Set AOI = [A2:A10] 'set to range to be formatted
For Each c In AOI
If IsDate(c) Then
dt = c.Value
Select Case Day(dt)
Case Is = 1, 21, 31
Suffix = "st"
Case Is = 2, 22
Suffix = "nd"
Case Is = 3, 23
Suffix = "rd"
Case Else
Suffix = "th"
End Select
c.NumberFormat = "d""" & Suffix & """ mmm yyyy"
End If
Next c
End Sub
=======================================
--ron