Share via

Word macro to change date format

Anonymous
2023-04-23T01:47:50+00:00

Hi,

I'm trying to create a macro to change dates from the format "the 12th of June" to "12 June", etc.

I found this piece of code that seems like a good starting point but I haven't been able to work out how to successfully modify it.

Thanks for any help!

Sub ChangeDateFormatWithReplaceCommand()

Dim myMonth(1 To 12) As String

myMonth(1) = "January"
myMonth(2) = "February"
myMonth(3) = "March"
myMonth(4) = "April"
myMonth(5) = "May"
myMonth(6) = "June"
myMonth(7) = "July"
myMonth(8) = "August"
myMonth(9) = "September"
myMonth(10) = "October"
myMonth(11) = "November"
myMonth(12) = "December"

For i = 1 To 12
    Selection.Find.ClearFormatting
    Selection.Find.Replacement.ClearFormatting
    With Selection.Find
        .Text = "(" & myMonth(i) & ")" & " ([0-9]{1,2}),"
        .Replacement.Text = "\2 \1"
        .Forward = True
        .Wrap = wdFindAsk
        .Format = False
        .MatchCase = False
        .MatchWholeWord = False
        .MatchWildcards = True
        .MatchSoundsLike = False
        .MatchAllWordForms = False
    End With
    Selection.Find.Execute Replace:=wdReplaceAll
Next i

End Sub

Microsoft 365 and Office | Word | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

Answer accepted by question author

Anonymous
2023-04-23T03:58:01+00:00

Change the search and replace lines as follows:

.Text = "the ([0-9]{1,2})[dhnrst]{2} of (" & myMonth(i) & ")"

.Replacement.Text = "\1 \2"

See https://www.gmayor.com/replace_using_wildcards.htm

Was this answer helpful?

2 people found this answer helpful.
0 comments No comments

0 additional answers

Sort by: Most helpful