I have a document with numbered sentences.
The numbers are out of sequence.
I'd like to make a macro to find and replace the numbers,
and replace them with an ordered sequence: 1, 2, 3, 4, 5 ...
The text looks like this:
1. John ate an apple. 3. He went to the store. 77. He bought candy. 104. He went home. 5. He played videos.
The best I was able to do was this:
Sub ReNumber()
'
Dim i As Integer
j = 0
For Each w In Documents(ThisDocument).Words
If IsNumeric(w) Then
i = i + 1
End If
Next w
For CNT = 1 To i
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "[0-9]{1,2}"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = True
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute
Selection.Font.ColorIndex = wdDarkRed
j = j + 1
With Selection.Find
.Replacement.Text = j
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = True
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceOne
Next CNT
End Sub
Any suggestions? I basically have given up at this point.
Thanks!
The numbers are out of sequence.
I'd like to make a macro to find and replace the numbers,
and replace them with an ordered sequence: 1, 2, 3, 4, 5 ...
The text looks like this:
1. John ate an apple. 3. He went to the store. 77. He bought candy. 104. He went home. 5. He played videos.
The best I was able to do was this:
Sub ReNumber()
'
Dim i As Integer
j = 0
For Each w In Documents(ThisDocument).Words
If IsNumeric(w) Then
i = i + 1
End If
Next w
For CNT = 1 To i
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "[0-9]{1,2}"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = True
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute
Selection.Font.ColorIndex = wdDarkRed
j = j + 1
With Selection.Find
.Replacement.Text = j
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = True
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceOne
Next CNT
End Sub
Any suggestions? I basically have given up at this point.
Thanks!