View Single Post
 
Old 04-22-2022, 04:15 AM
Bikram Bikram is offline Windows 10 Office 2007
Advanced Beginner
 
Join Date: Jul 2021
Location: Nepal
Posts: 94
Bikram is on a distinguished road
Default replacing_within_range

Hello members, I am using the code below posted below to find and replace within range that I have assigned to selection.range but the problem here is it not only replacing within range but beyond the range.
Code:
Sub nnewreplace()
Dim e(), f()
e = Array(" ", " ", Chr(160), Chr(9), "\(([a-z]{1,})\)")
f = Array("", "", "", "", "\1.")
Dim i As Integer
Dim rng As Range
Set rng = Selection.Range
For i = LBound(e) To UBound(e)
With rng.find
    .ClearFormatting
    .MatchWildcards = True
    .Text = e(i)
    .Replacement.Text = f(i)
    .Wrap = wdFindStop
End With
rng.find.Execute Replace:=wdReplaceAll
Next
End Sub
Here all replaced are within range except the last one. I looked by stepping through the code and I found that up to the second last item of the array e(i) the rng was the selection.range but when it passes rng.find.Execute Replace:=wdReplaceAll of last item the rng becomes "" . What can be done to avoid that problem? Any reply would be of great help.

Last edited by Bikram; 04-22-2022 at 04:24 AM. Reason: Added additional details
Reply With Quote