PDA

View Full Version : [SOLVED:] Macro to superscript ordinal date numbers' “st”, “nd”, “rd”, “th”



Restricted
10-23-2022, 01:24 AM
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

macropod
10-23-2022, 01:37 AM
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

Restricted
10-23-2022, 05:46 AM
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.

macropod
10-23-2022, 02:02 PM
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) (https://wordmvp.com/FAQs/General/UsingWildcards.htm)
Examples of wildcard characters (microsoft.com) (https://support.microsoft.com/en-us/office/examples-of-wildcard-characters-939e153f-bd30-47e4-a763-61897c87b3f4)
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.

Aussiebear
10-23-2022, 06:48 PM
Terrific explanation Paul. Well done.

Restricted
10-23-2022, 07:57 PM
Really useful to know, thanks for helping and sharing this - you are a legend in this community!