Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 10-08-2021, 09:08 PM
RobertDany RobertDany is offline Applying a VBA code only on a selected text or range Windows 7 64bit Applying a VBA code only on a selected text or range Office 2013
Novice
Applying a VBA code only on a selected text or range
 
Join Date: Jul 2021
Posts: 22
RobertDany is on a distinguished road
Default Applying a VBA code only on a selected text or range

Hello everyone,
I want to find and replace certain word in selected range of text only,
Also, if I don't select anything, a message appear "Please select the range first", in order to prevent find and replace on entire document by mistake.


Code:
Sub ReplaceName()
'
'Find what Format:
    Selection.Find.ClearFormatting
    
' Replace with Format:
    Selection.Find.Replacement.ClearFormatting
    With Selection.Find
        .Text = "Jhon"
        .Replacement.Text = "Robert"
        .Forward = True
        .Wrap = wdFindContinue
        .Format = True
        .MatchCase = False
        .MatchWholeWord = False
        .MatchKashida = False
        .MatchDiacritics = False
        .MatchAlefHamza = False
        .MatchControl = False
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
    End With
    Selection.Find.Execute Replace:=wdReplaceAll
End Sub
Thank you
Reply With Quote
  #2  
Old 10-08-2021, 09:23 PM
gmayor's Avatar
gmayor gmayor is offline Applying a VBA code only on a selected text or range Windows 10 Applying a VBA code only on a selected text or range Office 2019
Expert
 
Join Date: Aug 2014
Posts: 4,101
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 of
Default

That's fairly straightforward and I assume you know that you have spelled 'John' incorrectly?
You only need to change the line
Code:
.Wrap = wdFindContinue
to
Code:
.Wrap = wdFindStop
however
Code:
Sub ReplaceName()
Dim oRng As Range
    If Len(Selection.Range) = 0 Then
        MsgBox "Select the text first", vbCritical
        Exit Sub
    End If
    Set oRng = Selection.Range
    With oRng.Find
        .ClearFormatting
        .Replacement.ClearFormatting
        .Text = "Jhon"
        .Replacement.Text = "Robert"
        .Forward = True
        .Wrap = wdFindStop
        .Format = True
        .MatchCase = False
        .MatchWholeWord = False
        .MatchKashida = False
        .MatchDiacritics = False
        .MatchAlefHamza = False
        .MatchControl = False
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
        .Execute Replace:=wdReplaceAll
    End With
    Set oRng = 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
  #3  
Old 10-09-2021, 08:31 AM
RobertDany RobertDany is offline Applying a VBA code only on a selected text or range Windows 7 64bit Applying a VBA code only on a selected text or range Office 2013
Novice
Applying a VBA code only on a selected text or range
 
Join Date: Jul 2021
Posts: 22
RobertDany is on a distinguished road
Default

Quote:
Originally Posted by gmayor View Post
That's fairly straightforward and I assume you know that you have spelled 'John' incorrectly?
[/CODE]
Ohh, I am sorry for this mistake

However, Thank you so much
Best Regards with special thanks
Reply With Quote
Reply

Tags
find & replace, vba, word vba

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Vertically Center Selected Range in the Useable Window gmaxey Word VBA 0 12-19-2020 10:01 AM
Applying a VBA code only on a selected text or range Find and Replace Selected Text or Macro for finding selected text mrplastic Word VBA 4 12-20-2019 01:25 PM
Applying a VBA code only on a selected text or range Set Range based on selected rows. 14spar15 Excel Programming 8 11-19-2018 08:08 AM
Two problems: Applying bold affects other tables and canvas doesn't appear at specified range slaycock Word VBA 6 12-08-2016 02:28 AM
Applying a VBA code only on a selected text or range Import a specific range (bookmarked section) from all Word docs in a selected folder Greengecko Word VBA 5 06-14-2016 07:54 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 04:25 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft