Consulting

Results 1 to 6 of 6

Thread: Macro to superscript ordinal date numbers' “st”, “nd”, “rd”, “th”

  1. #1

    Macro to superscript ordinal date numbers' “st”, “nd”, “rd”, “th”

    Hi Everyone

    I am editing a bunch of my documents which have had its formatting removed, and need some help as to whether it is possible to have a macro that would automatically go through my Word document and replace all non-superscripted date suffixes (e.g. "th" or "st" or "nd" or "rd").

    e.g. when typing dates such as "July 4th"? I want it to look like July
    4th


    Or instead of 23rd of September, I need it to be 23rd of September...

    Right now I am having to go through each entry and press 'space' on the keyboard after the letter 'h' (e.g. for '4th') so that Word 'automatically' superscripts it, but there may be 100+ entries I need to do it for which takes a lot of time.

    Any help here would be great.

    Thank you

  2. #2
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    For example:
    Sub Demo()
    Application.ScreenUpdating = False
    With ActiveDocument.Range
      With .Find
        .ClearFormatting
        .Replacement.ClearFormatting
        .Text = "[0-9][snrt][tdh]"
        .Replacement.Text = ""
        .Format = False
        .Forward = True
        .Wrap = wdFindStop
        .MatchWildcards = True
      End With
      Do While .Find.Execute
        .Start = .Start + 1
        .Font.Superscript = True
        .Collapse wdCollapseEnd
      Loop
    End With
    Application.ScreenUpdating = True
    End Sub
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  3. #3
    Thanks Paul, works great - how do you generate these RegEx expressions - I never understood how you come up with them. "[0-9][snrt][tdh]"

    e.g. Why didn't you do "[snrt][tddh]", because it almost looked like each nth element inside the 1st square bracket set, was paired up with the corresponding nth element in the 2nd set of square brackets - until I saw the 2nd square bracket set only had 3 elements.
    Last edited by macropod; 10-24-2022 at 05:12 AM. Reason: Deleted unnecessary quote of entire post replied to

  4. #4
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    Quote Originally Posted by Restricted View Post
    how do you generate these RegEx expressions - I never understood how you come up with them. "[0-9][snrt][tdh]"

    e.g. Why didn't you do "[snrt][tddh]", because it almost looked like each nth element inside the 1st square bracket set, was paired up with the corresponding nth element in the 2nd set of square brackets - until I saw the 2nd square bracket set only had 3 elements.
    See, for example:
    Finding and replacing characters using wildcards (wordmvp.com)
    Examples of wildcard characters (microsoft.com)
    In the F/R code I posted:
    • [0-9] says to find any digit.
    • [snrt] says to find any of n, s, r, or t.
    • [tdh] says to find any of d, h, or t.
    so, [0-9][snrt][tdh] says to find any digit followed by any of n, s, r, or t, followed by any of d, h, or t. The order of the elements and the number of elements in each group is inconsequential.
    I could just as easily have used [0-9][dhnrst]{2}, which would say to find any digit followed by any two of d, h, n, s, r, or t, but I chose to be more explicit about which characters in sequence had to be found.
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  5. #5
    Moderator VBAX Guru Aussiebear's Avatar
    Joined
    Dec 2005
    Location
    Queensland
    Posts
    4,997
    Location
    Terrific explanation Paul. Well done.
    Remember To Do the Following....
    Use [Code].... [/Code] tags when posting code to the thread.
    Mark your thread as Solved if satisfied by using the Thread Tools options.
    If posting the same issue to another forum please show the link

  6. #6
    Really useful to know, thanks for helping and sharing this - you are a legend in this community!
    Last edited by macropod; 10-24-2022 at 05:12 AM. Reason: Deleted unnecessary quote of entire post replied to

Tags for this Thread

Posting Permissions

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