Microsoft Office Forums

Go Back   Microsoft Office Forums > >

 
 
Thread Tools Display Modes
Prev Previous Post   Next Post Next
  #8  
Old 01-26-2025, 11:03 PM
macropod's Avatar
macropod macropod is offline Help, How to have only a selected text to change = Range Windows 10 Help, How to have only a selected text to change = Range Office 2016
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 22,469
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

In which case you could use:
Code:
Sub Demo()
Application.ScreenUpdating = False
Dim Rng As Range, StrYY As String, StrMM As String, StrDD As String
Application.CheckLanguage = True
With Selection
  Set Rng = .Range
  .LanguageID = wdFrenchCanadian
  .NoProofing = False
  With .Range
    With .Find
      .ClearFormatting
      .Replacement.ClearFormatting
      .Text = "<[JFMASOND][a-z.]{2;9} [0-9]{1;2}, [0-9]{2;4}>"
      .Replacement.Text = ""
      .MatchWildcards = True
      .Wrap = wdFindStop
      .Forward = True
      .Format = False
    End With
    Do While .Find.Execute
      If .InRange(Rng) Then
        StrYY = Split(.Text, " ")(2): StrMM = Left(Split(.Text, " ")(0), 3): StrDD = Split(Split(.Text, " ")(1), ",")(0)
        If Len(StrYY) = 2 Then StrYY = "20" & StrYY
         Select Case StrMM
          Case "Jan": StrMM = " janv. "
          Case "Feb": StrMM = " févr. "
          Case "Mar": StrMM = " mars "
          Case "Apr": StrMM = " avr. "
          Case "May": StrMM = " mai "
          Case "Jun": StrMM = " juin "
          Case "Jul": StrMM = " juill. "
          Case "Aug": StrMM = " août "
          Case "Sep": StrMM = " sept. "
          Case "Oct": StrMM = " oct. "
          Case "Nov": StrMM = " nov. "
          Case "Dec": StrMM = " déc. "
        End Select
        .Text = StrDD & StrMM & StrYY
        .Font.Color = 16711937 'Blue HTML 1/1/255
      Else
        Exit Do
      End If
      .Collapse wdCollapseEnd
    Loop
  End With
End With
Application.ScreenUpdating = True
End Sub
With the Find expression:
• <[JFMASOND][a-z.]{2;9} looks for the month, with or without the abbreviation period and regardless of whether it's in a short date or long date format;
• [0-9]{1;2}, looks for the day; and
• [0-9]{2;4}> looks for the year in 2-digit to 4-digit format.
The Split function can be used to separate out different elements as a string. By splitting the string at each space, the various parts of the date can be differentiated as if they're a 0-based array. So, given the Find pattern:
• Split(.Text, " ")(0) gets the month;
• Split(.Text, " ")(1) gets the day. Nesting it, thus Split(Split(.Text, " ")(1), ",")(0), eliminates the comma, if present; and
• Split(.Text, " ")(2) gets the year
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
 

Tags
help me, range, selection



Similar Threads
Thread Thread Starter Forum Replies Last Post
Count lines within selected text range eduzs Word VBA 4 09-17-2023 05:22 PM
Change background of selected text ultrarunner2020 Word 5 03-10-2023 02:59 PM
Applying a VBA code only on a selected text or range RobertDany Word VBA 2 10-09-2021 08:31 AM
How to change color indicating selected text daylee Word 1 03-27-2019 01:31 PM
Please help. I want to change the text of a cell when another is selected frankzelnik Excel 5 06-19-2018 01:34 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 08:58 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2025, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft