View Single Post
 
Old 10-29-2021, 09:44 PM
Guessed's Avatar
Guessed Guessed is offline Windows 10 Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 4,176
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

Try this code. Note that the hyphen isn't a plain hyphen - it is an n-dash which has an Asc value of 150. A regular hyphen has an Asc value of 45.
Code:
Sub FindPattern()
  Dim aRng As Range, str As String, int1 As Integer, int2 As Integer, arrStr() As String
  Set aRng = ActiveDocument.Range
  With aRng.Find
    .ClearFormatting
    .MatchWildcards = True
    .Text = "[0-9]{3}–[0-9]{1}"
    Do While .Execute = True
      arrStr = Split(aRng.Text, "–")
      int1 = CInt(arrStr(0)) + 2
      int2 = CInt(arrStr(1)) + 2
      aRng.Text = int1 & "-" & int2
      aRng.Collapse Direction:=wdCollapseEnd
      aRng.End = ActiveDocument.Range.End
    Loop
  End With
End Sub
I note you didn't answer the question of what happens with the last number if it is an 8 or 9 so I didn't make an effort to handle that case differently.
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote