Date Formatter Macro

myith29

New Member
Joined
Apr 19, 2014
Messages
41
Hello,

I would like to create a date formatter macro that can be used in multiple worddocuments. I need the macro to do threethings:


  • Take a highlighted short date like 6/17/19and convert it to the long date Monday, June 17, 2019;
  • Have a shortcut key attached (such asCtrl+Shift+D); and
  • I need the macro to be saved as anindividual program so I can run it with any Word document I open it in and notjust the one I created it in.
Any help on this would be greatly appreciated.
Thank you,

 

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
Here's a sub that would look at each "word" in a selection, checking for a date value, and convert it to the long date. You could assign the shortcut, then save the document as an add-in.

Code:
Sub FixDate()

Dim arr As Variant, i&
arr = Split(Selection.Text, " ")
For i = LBound(arr) To UBound(arr)
    If IsDate(arr(i)) Then arr(i) = Format(arr(i), "DDDD MMMM DD, YYYY")
Next i
Selection.Text = Join(arr, " ")
End Sub
 
Upvote 0
Here's a different approach that won't mess with the formatting of any surrounding text. As coded, it processes every date in the selected range.
Code:
Sub LongDates()
Application.ScreenUpdating = False
Dim Rng As Range
With Selection
  Set Rng = .Range
  With .Range
    With .Find
      .ClearFormatting
      .Replacement.ClearFormatting
      .Text = "<[0-9]{1,2}/[0-9]{1,2}/[0-9]{2,4}>"
      .Replacement.Text = ""
      .Forward = True
      .Format = False
      .Wrap = wdFindStop
      .MatchWildcards = True
      .Execute
    End With
    Do While .Find.Found
      If .InRange(Rng) = False Then Exit Do
      If IsDate(.Text) Then .Text = Format(.Text, "DDDD, MMMM D, YYYY")
      .Collapse wdCollapseEnd
      .Find.Execute
    Loop
  End With
End With
Application.ScreenUpdating = True
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,164
Messages
6,170,444
Members
452,326
Latest member
johnshaji

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top