Home Excel Powerpoint Word Outlook Error Codes

VBA Useful Codes

Welcome to the World of VBA
Home > VBAUsefulCodes

Add st,nd,rd and th to date

If you want to automatically insert st,nd,rd or th to the date like Ist, 2nd, 3rd etc then use the following example to achieve


Input: 22-Dec-2018
Output: 22nd

sub dd()

    frmdate = "01-Dec-2018"

    dt_dd_new = DayEnding(frmdate)

    Msgbox DayEnding(frmdate)

End sub

Function DayEnding(FormatDate As Variant) As String

    Dim DayOfMonth As String

    Dim Ext As String


    DayOfMonth = Day(FormatDate)


    Select Case DayOfMonth

        Case 1, 21, 31: Ext = "st"

        Case 2, 22:     Ext = "nd"

        Case 3, 23:     Ext = "rd"

        Case Else:      Ext = "th"

    End Select

    DayEnding = DayOfMonth & Ext

End Function



Enter your e-mail below and get notified on the latest blog posts.


VBA Outlook KPI Excel Alt+F11 Dashboards Macros Recording Graphs Automation Developer WaterFall Powerpoint Charts Pivot Tables Forecast Charts

Follow Us