Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 04-22-2022, 04:15 AM
Bikram Bikram is offline replacing_within_range Windows 10 replacing_within_range Office 2007
Advanced Beginner
replacing_within_range
 
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
  #2  
Old 04-25-2022, 07:35 AM
Peterson Peterson is offline replacing_within_range Windows 10 replacing_within_range Office 2019
Competent Performer
 
Join Date: Jan 2017
Posts: 143
Peterson is on a distinguished road
Default

Try the code below. By the way, you are searching for spaces twice.

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
            .Execute Replace:=wdReplaceAll
        End With
    Next
End Sub
Reply With Quote
  #3  
Old 04-25-2022, 08:24 PM
Bikram Bikram is offline replacing_within_range Windows 10 replacing_within_range Office 2007
Advanced Beginner
replacing_within_range
 
Join Date: Jul 2021
Location: Nepal
Posts: 94
Bikram is on a distinguished road
Default

Thank you, Peterson, for your suggestion. But the code I posted and the code you replied to seem the same.
Reply With Quote
  #4  
Old 04-25-2022, 08:27 PM
Bikram Bikram is offline replacing_within_range Windows 10 replacing_within_range Office 2007
Advanced Beginner
replacing_within_range
 
Join Date: Jul 2021
Location: Nepal
Posts: 94
Bikram is on a distinguished road
Default

One thing that I am wondering here is why is it replacing all the items within the selection and only the last item is being replaced outside the selection. Isn't there any way to constrain the replacement within the selection or range?
Reply With Quote
  #5  
Old 04-25-2022, 10:44 PM
Peterson Peterson is offline replacing_within_range Windows 10 replacing_within_range Office 2019
Competent Performer
 
Join Date: Jan 2017
Posts: 143
Peterson is on a distinguished road
Default

No, it's not the same.

Your code wasn't working because the Execute method was outside of the With statement.
Reply With Quote
  #6  
Old 04-26-2022, 12:04 AM
Bikram Bikram is offline replacing_within_range Windows 10 replacing_within_range Office 2007
Advanced Beginner
replacing_within_range
 
Join Date: Jul 2021
Location: Nepal
Posts: 94
Bikram is on a distinguished road
Default

Thanks!! Peterson.
Reply With Quote
  #7  
Old 04-26-2022, 12:32 AM
Guessed's Avatar
Guessed Guessed is offline replacing_within_range Windows 10 replacing_within_range Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 4,158
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

There is also the potential that the rng is collapsing after the first pass. In my experience, when you use rng.Find the rng collapses to the found instances so a second pass may not include the rng you thought you were looking for.

To avoid this you could redefine the rng before each pass.
Code:
Sub nnewreplace()
    Dim e(), f()
    e = Array(" ", " ", Chr(160), Chr(9), "\(([a-z]{1,})\)")
    f = Array("", "", "", "", "\1.")
    Dim i As Integer, rng As Range
    
    For i = LBound(e) To UBound(e)
        Set rng = Selection.Range
        With rng.Find
            .ClearFormatting
            .MatchWildcards = True
            .Text = e(i)
            .Replacement.Text = f(i)
            .Wrap = wdFindStop
            .Execute Replace:=wdReplaceAll
        End With
    Next
End Sub
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote
  #8  
Old 04-26-2022, 01:22 AM
Bikram Bikram is offline replacing_within_range Windows 10 replacing_within_range Office 2007
Advanced Beginner
replacing_within_range
 
Join Date: Jul 2021
Location: Nepal
Posts: 94
Bikram is on a distinguished road
Default

Thank you very much Andrew, for the reply. I tried to redefine the rng each time as you did but it still replaces the whole document below the selection. The rng collapses mainly on the 4th item after passing .Execute Replace:=wdReplaceAll before this line the rng was the original range. But when the code passes this line on the 4th item I got the rng = "". I couldn't find any explainable reason on the internet so I tried the following code of line temporarily
Code:
    If i = 4 Then
        .Execute Replace:=wdReplaceOne
    Else
        .Execute Replace:=wdReplaceAll
    End If
Reply With Quote
  #9  
Old 04-26-2022, 02:03 AM
Guessed's Avatar
Guessed Guessed is offline replacing_within_range Windows 10 replacing_within_range Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 4,158
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

The code I provided looks like it sticks to the selection so I'm not sure why it is failing for you.

Is the first/last character in your selection one of the found items?
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote
  #10  
Old 04-26-2022, 04:46 AM
Bikram Bikram is offline replacing_within_range Windows 10 replacing_within_range Office 2007
Advanced Beginner
replacing_within_range
 
Join Date: Jul 2021
Location: Nepal
Posts: 94
Bikram is on a distinguished road
Default

If the found item is the first character of the selection then wdreplaceall replaces beyond the selection if the found item is other than the first character then the code works fine and only replaces within the selection.
Reply With Quote
Reply



Other Forums: Access Forums

All times are GMT -7. The time now is 11:30 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