I want a macro to convert date format so that “11/5/2011”, for example, or “11/05/2011” would become “11 May 2011”.
Instead the macro I'm trying out gives me the following:
11/5/2011 | should be | 11 May 2011 | but instead I get | 5 November 2011 |
12/5/2011 | should be | 12 May 2011 | but instead I get | 5 December 2011 |
13/5/2011 | should be | 13 May 2011 | but instead I get | 13 May 2011 |
14/5/2011 | should be | 14 May 2011 | but instead I get | 14 May 2011 |
The macro code is given below, and I would be most grateful if someone can tell me how to correct it.
Sub ConvertDateFormat()
Dim FoundOne As Boolean
Selection.HomeKey Unit:=wdStory, Extend:=wdMove
FoundOne = True ' loop at least once
Do While FoundOne ' loop until no date is found
With Selection.Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = "([0-9]{1,2})[/]([0-9]{1,2})[/]([0-9]{4})"
.Format = True
.Forward = True
.MatchWildcards = True
End With
Selection.Find.Execute Replace:=wdReplaceNone
' check that the find is a date
If IsDate(Selection.Text) Then
Selection.Text = Format(Selection.Text, "d MMMM yyyy")
Selection.Collapse wdCollapseEnd
Else ' not a date - end loop
FoundOne = False
End If
Loop
End Sub
I would also like to check how I should alter the code to give me a version of the macro that does not loop but simply finds the next date, so that I would then click the macro’s button each time to execute and go to next. This would be useful in cases where I prefer to check each instance rather than just run the macro for the full document.
My regional settings in Windows (8.1) are as follows:
Language: English (South Africa)
Short date: yyy-MM-dd
Long date: dd MMMM yyyy