Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 11-12-2021, 01:02 AM
Bikram Bikram is offline find and replace only in current selection Windows 10 find and replace only in current selection Office 2007
Advanced Beginner
find and replace only in current selection
 
Join Date: Jul 2021
Location: Nepal
Posts: 94
Bikram is on a distinguished road
Default find and replace only in current selection

Greeting to all,
I have been using find and replace through vba. It works fine except, sometimes it replaces whole document instead of just replacing the selection. The code I am using is below:


Sub Macro32()
'
' Macro32 Macro
'
'

With selection
.Find.ClearFormatting
.Find.Replacement.ClearFormatting
End with
With Selection.Find
.Text = "^t"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindStop
.MatchWildcards = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
With selection
.Find.ClearFormatting
.Find.Replacement.ClearFormatting
End with
With Selection.Find
.Text = "(=)"
.Replacement.Text = "^t\1"
.Forward = True
.Wrap = wdFindStop
.MatchWildcards = True
End With
Selection.Find.Execute Replace:=wdReplaceAll
End Sub

How could I modify this code to just replace the selected area and nothing else? Looking forward for suggestions. Thanks in advance.
Reply With Quote
  #2  
Old 11-12-2021, 02:10 AM
gmayor's Avatar
gmayor gmayor is offline find and replace only in current selection Windows 10 find and replace only in current selection Office 2019
Expert
 
Join Date: Aug 2014
Posts: 4,138
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

You could simplify it and the following should only apply to the selected text:
Code:
Sub Macro32()
    With Selection.Find
        .ClearFormatting
        .Replacement.ClearFormatting
        .Text = "^t"
        .Replacement.Text = ""
        .Forward = True
        .Wrap = wdFindStop
        .MatchWildcards = False
        .Execute Replace:=wdReplaceAll
        .Text = "="
        .Replacement.Text = "^t^&"
        .Execute Replace:=wdReplaceAll
    End With
End Sub
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote
  #3  
Old 11-12-2021, 03:08 AM
Bikram Bikram is offline find and replace only in current selection Windows 10 find and replace only in current selection Office 2007
Advanced Beginner
find and replace only in current selection
 
Join Date: Jul 2021
Location: Nepal
Posts: 94
Bikram is on a distinguished road
Default

Thank you sir, for the reply!!
It works great but now also it replaces outside the selection to the end of the document but when I select only the characters of and apply then only it replaces in the selection but if I select the whole paragraph then it replaces outside of the selection.

I have attached a sample file to experiment.
Attached Files
File Type: docx Sample.docx (12.7 KB, 8 views)
Reply With Quote
  #4  
Old 11-12-2021, 04:44 AM
gmayor's Avatar
gmayor gmayor is offline find and replace only in current selection Windows 10 find and replace only in current selection Office 2019
Expert
 
Join Date: Aug 2014
Posts: 4,138
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

Hmmm.
The following works with your document
Code:
Sub Macro32()
Dim oRng As Range, oDup As Range

    Set oRng = Selection.Range
    Set oDup = Selection.Range
    With oRng.Find
        .ClearFormatting
        .Replacement.ClearFormatting
        .Text = "^t"
        .Forward = True
        .Wrap = wdFindStop
        .MatchWildcards = False
        Do While .Execute
            If oRng.InRange(Selection.Range) Then
                oRng.Text = ""
            End If
            oRng.Collapse 0
        Loop
    End With
    
    Set oRng = oDup.Duplicate
    With oRng.Find
        .Text = "="
        Do While .Execute
            If oRng.InRange(oDup) Then
                oRng.Text = vbTab & oRng.Text
            End If
            oRng.Collapse 0
        Loop
    End With
    Set oRng = Nothing
    Set oDup = Nothing
End Sub
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote
  #5  
Old 11-12-2021, 07:31 AM
Bikram Bikram is offline find and replace only in current selection Windows 10 find and replace only in current selection Office 2007
Advanced Beginner
find and replace only in current selection
 
Join Date: Jul 2021
Location: Nepal
Posts: 94
Bikram is on a distinguished road
Default

Thank you!!
Reply With Quote
Reply

Tags
find



Similar Threads
Thread Thread Starter Forum Replies Last Post
How do you use the find and replace tool to find dates and times in Excel 2013? Jules90 Excel 3 04-14-2020 07:40 PM
In Find and Replace, can Word stop after each Replace? wardw Word 1 06-08-2017 02:47 PM
find and replace only in current selection Find what box in Find and replace limits the length of a search term Hoxton118 Word VBA 7 06-10-2014 05:05 AM
find and replace only in current selection Bad view when using Find and Find & Replace - Word places found string on top line paulkaye Word 4 12-06-2011 11:05 PM
Automatic find replace after selection in dropdown vsempoux Word 0 10-28-2009 08:45 AM

Other Forums: Access Forums

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