Date Range

J

jerem

When I use the following macro, what reference range is being employed that
makes certain decisions for the conversion? For example, when I have a date
of 1/12/46, this converts to January 12, 1946, not January 12, 2046; when I
have a date of 1/14/20, this converts to January 14, 2020, not January 14,
1920; when I have a date of 2/3//10 this converts to February 3, 2010 not
February 3, 1910??? Seems like this was only project so far into the future.
I haven't tried all numbers but once you reach that boundary it assigns the
20th century to any number there and above.

Sub NumberDatesToWordDates()
Dim oRng As Word.Range
Set oRng = ActiveDocument.Range
With oRng.Find
.Text = "[0-9]{1,}/[0-9]{1,}/[0-9]{2,}"
.MatchWildcards = True
While .Execute
If IsDate(oRng.Text) Then
oRng = Format(oRng, "MMMM d, yyyy")
oRng.Collapse wdCollapseEnd
End If
Wend
End With
End Sub
 
G

Greg Maxey

The range is though 29. Anything before that is converted to 20##,
anything before is 19##. You could force 20## using:

Sub NumberDatesToWordDates()
oRng = Format(oRng, "MMMM d, 20yy")
 
M

macropod

Hi jerem,

If you only supply 2-digit years, Word has to guess which centuries you mean. There is an algorithm for that, but that doesn't mean
it'll return the dates you want. The safest approach is to use only 4-digit years. If that's not possible, you'll need to add your
own logic to the process to determine and apply the correct century. Likewise, if your days and months are only supplied as numbers,
Word will assume they're ordered according to your system's regional settings, which may or may not be the case.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top