View Single Post
 
Old 04-10-2014, 07:05 PM
macropod's Avatar
macropod macropod is offline Windows 7 32bit Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 22,467
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

If you only want to find numbers that increment try:
Code:
Sub Demo()
Dim Rslt, Rng As Range, i As Long
Set Rng = Selection.Range
With ActiveDocument.Range
  With .Find
    .ClearFormatting
    .Replacement.ClearFormatting
    .Text = "[0-9]{1,}"
    .Replacement.Text = ""
    .Forward = True
    .Wrap = wdFindStop
    .Format = False
    .MatchWildcards = True
    .Execute
  End With
  Do While .Find.Found
    If .Text = i + 1 Then
      .Select
      Rslt = MsgBox("Replace this instance of:" & vbCr & .Text, vbYesNoCancel)
      If Rslt = vbCancel Then Exit Sub
      If Rslt = vbYes Then
        .Text = vbNullString
        i = i + 1
      End If
    ElseIf .Text = i Then
      .Select
      Rslt = MsgBox("Replace this instance of:" & vbCr & .Text, vbYesNoCancel)
      If Rslt = vbCancel Then Exit Sub
      If Rslt = vbYes Then .Text = vbNullString
    End If
    .Collapse wdCollapseEnd
    .Find.Execute
  Loop
End With
Rng.Select
End Sub
Note: As coded, the macro includes an ElseIf test to pick up repeats of the last number. You can delete that if you don't want it.

I'm not sure what you mean by "Is there a way I could use a regular expression to do the search that would exclude numerals to the left or right of my target?" The code in both this post and my previous one (both of which use wildcards, which are Word's equivalent of regular expressions) only picks up whole numbers anyway - it won't find just the 1 or 11 within 110 or 211 or 123114, for example.

PS: When posting code, please use the code tags. They're on the 'Go Advanced' tab at the bottom of this screen.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote