View Single Post
 
Old 10-08-2021, 09:08 PM
RobertDany RobertDany is offline Windows 7 64bit Office 2013
Novice
 
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