Microsoft Office Forums

Go Back   Microsoft Office Forums > >

 
 
Thread Tools Display Modes
Prev Previous Post   Next Post Next
  #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
 

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 03:57 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