Microsoft Office Forums

Go Back   Microsoft Office Forums > >

 
 
Thread Tools Display Modes
Prev Previous Post   Next Post Next
  #9  
Old 01-27-2025, 07:02 AM
gmaxey gmaxey 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 2019
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,636
gmaxey is just really nicegmaxey is just really nicegmaxey is just really nicegmaxey is just really nicegmaxey is just really nice
Default

Paul is undisputed absolute master of using and keeping track of complex split or replace strings and yes they work very well!


The following is by no means superior, but you might find it easier to understand:

Code:
Sub Modified_MacroPod_Demo()
'A few more variables
Dim varParts
Dim varMonths
Dim LS As String
Dim lngMonthIndex As Long
Dim oRng As Range, oRngCheck As Range
  Application.ScreenUpdating = False
  Application.CheckLanguage = True
  'Get the regional list separator for use in the find string.
  LS = CreateObject("WScript.Shell").RegRead("HKEY_CURRENT_USER\Control Panel\International\sList")
  'Define the various month replacments in a string, then convert to an array.
  varMonths = Split("janv.|févr.|mars|avr.|mai|juin|juill.|août|sept.|oct.|nov.|déc.", "|")
  Set oRng = Selection.Range
  
  With oRng
    Set oRngCheck = .Duplicate
    .LanguageID = wdFrenchCanadian
    .NoProofing = False
  End With
  With oRng.Find
    .ClearFormatting
    .Replacement.ClearFormatting
    'Modified using the regional list separator character.
    .Text = "<[JFMASOND][a-z.]{2" & LS & "9} [0-9]{1" & LS & "2}, [0-9]{2" & LS & "4}>"
    .Replacement.Text = ""
    .MatchWildcards = True
    .Wrap = wdFindStop
    .Forward = True
    .Format = False
    Do While .Execute
      With oRng
        If .InRange(oRngCheck) Then
          'Create an array of the date parts i.e., day, month, year.
          varParts = Split(oRng.Text, " ")
          'Find the index of the month part.
          lngMonthIndex = Month("1 " & (Left(varParts(0), 3)) & " 2025")
          'If the year part is only 2 digits, prefix with "20"
          If Len(varParts(2)) = 2 Then varParts(2) = "20" & varParts(2)
          'Build the replacement string.
          'a) The "day" part will include the comma.  Strip it out: Replace(varParts(1), ",", "")
          'b) The "month" part will be pulled from varMonths defined above: varMonths(lngMonthIndex - 1)
          .Text = Replace(varParts(1), ",", "") & " " & varMonths(lngMonthIndex - 1) & " " & varParts(2)
          .Font.Color = 16711937
          .Collapse wdCollapseEnd
        Else
          Exit Do
        End If
      End With
    Loop
  End With
  Application.ScreenUpdating = True
lbl_Exit:
  Exit Sub
End Sub
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/

Last edited by gmaxey; 01-27-2025 at 12:38 PM.
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 02:52 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