Formatting dates with st, nd, rd etc?

merlin777

Well-known Member
Joined
Aug 29, 2009
Messages
1,397
Office Version
  1. 2007
Is there a way to format dates so instead of Monday 03 January it says Monday 3rd January and so on?
 

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
Perhaps this in the sheet's code module
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    Dim oneCell As Range
    For Each oneCell In Target
        With oneCell
            If .Column = 3 Then
                If IsDate(.Value) Then
                    Select Case Day(.Value)
                        Case 1, 21, 31
                            .NumberFormat = "dddd d""st"" mmm"
                        Case 2, 22
                            .NumberFormat = "dddd d""nd"" mmm"
                        Case 3, 23
                            .NumberFormat = "dddd d""rd"" mmm"
                        Case 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 24, 25, 26, 27, 28, 29, 30
                            .NumberFormat = "dddd d""th"" mmm"
                    End Select
                End If
            End If
        End With
    Next oneCell
End Sub
 
Upvote 0
thanks for this.

I went to sheet tab/view code and pasted your code.

I inserted 10 consecutive dates and formatted them e.g. 'Monday 01 January' but there didn't seem to be any effect from the code. am i doing something wrong?
 
Upvote 0
i just realised it works on column 3 - works beautifully!

is there a tweak to apply it to a range of columns e'g. B7 to F7?
 
Upvote 0
ah, i see i'm wrong 1 it work son any cell but only when the cell is changed.

I just out the code in the where it is to end up and now its not working.

I tried tpying a date in an empty cell '1/1/2014' and there was no change on this or the existing date cells.

of the 5 column headings for 5 dates, mon, B7, gets it's contents from a formula:
=FIRSTDAYOFYEAR+(7*$C$2)-7
and the other 4 days are =b7+1,= c7+1 etc

am i doing something wrong?
 
Upvote 0
This should work
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    Dim oneCell As Range
    Dim keyRange As Range
    Set keyRange = Range("B7:F7")
    
    On Error Resume Next
    Set keyRange = Application.Union(keyRange, keyRange.Precedents)
    On Error GoTo 0
    
    If Not Application.Intersect(Target, keyRange) Is Nothing Then
        For Each oneCell In Application.Intersect(Target, keyRange)
            With oneCell
                    If IsDate(.Value) Then
                        Select Case Day(.Value)
                            Case 1, 21, 31
                                .NumberFormat = "dddd d""st"" mmm"
                            Case 2, 22
                                .NumberFormat = "dddd d""nd"" mmm"
                            Case 3, 23
                                .NumberFormat = "dddd d""rd"" mmm"
                            Case 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 24, 25, 26, 27, 28, 29, 30
                                .NumberFormat = "dddd d""th"" mmm"
                        End Select
                    End If
            End With
        Next oneCell
    End If
End Sub
 
Upvote 0
I'm trying to understand what cusses the changes to apply.

I started with a new sheet/book
I entered a date by hand '10/8/2014' and it displayed as '10/8/2014'
I reformatted manually to 'Monday 10 August' and that's how it stayed
I cut and pasted it to another cell - no change
I copied and pasted it to another cell and the 'th' appeared
and now, it doesn't matter what I try I can't enter a date and have the format change automatically

...mystified

How should it work, mike? Ideally it need to work on a small range of cells which show data from another sheet using index/row/column and not from manual entry.
 
Upvote 0
When the user enters a value in a cell, that cell triggers a Change Event.
If there is another cell that contains a formula that refers to the first cell, that formula cell does not trigger a Change

The code in post #7 looks to see if B7:F7 or their precedents has changed. And I see where my logic was faulty.

Just to be clear:
The only cells where you want ordinal dates are B7:F7? Correct?
Some of those cells have user entered values some have formulas? Correct?
 
Upvote 0

Forum statistics

Threads
1,214,653
Messages
6,120,750
Members
448,989
Latest member
mariah3

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