Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 01-13-2021, 03:04 AM
alex100 alex100 is offline Find and replace only the last match Windows 7 64bit Find and replace only the last match Office 2016
Advanced Beginner
Find and replace only the last match
 
Join Date: May 2020
Posts: 69
alex100 is on a distinguished road
Default Find and replace only the last match

The code below will find and replace all keywords in a selection.

Code:
With Selection.Find
    .ClearFormatting
    .Text = "keyword"
    .Replacement.Text = "replacement_keyword"
    .Forward = True
    .Wrap = wdFindContinue
    .Format = False
    .MatchCase = False
    .MatchWholeWord = False
    .MatchWildcards = False
    .MatchSoundsLike = False
    .MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
How can I rewrite it so that it will only replace the last keyword in the selection? So, if it happens there are multiple keywords in the text, only the last match should be replaced, while the others before it should not be changed.



Alex
Reply With Quote
  #2  
Old 01-13-2021, 03:10 AM
macropod's Avatar
macropod macropod is offline Find and replace only the last match Windows 10 Find and replace only the last match Office 2016
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 20,738
macropod has a brilliant futuremacropod has a brilliant futuremacropod has a brilliant futuremacropod has a brilliant futuremacropod has a brilliant futuremacropod has a brilliant futuremacropod has a brilliant futuremacropod has a brilliant futuremacropod has a brilliant futuremacropod has a brilliant futuremacropod has a brilliant future
Default

That's as simple as changing:
.Forward = True
to
.Forward = False
and:
wdReplaceAll
to:
wdReplaceOne
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #3  
Old 01-13-2021, 07:27 AM
alex100 alex100 is offline Find and replace only the last match Windows 7 64bit Find and replace only the last match Office 2016
Advanced Beginner
Find and replace only the last match
 
Join Date: May 2020
Posts: 69
alex100 is on a distinguished road
Default

Quote:
Originally Posted by macropod View Post
That's as simple as changing...
Thank you, Paul! Just what I needed...

Indeed, very simple, but only for those that know their craft! For the rest of us (starting with me), things can be tricky and time consuming... ;-)

Alex
Reply With Quote
  #4  
Old 01-13-2021, 01:06 PM
macropod's Avatar
macropod macropod is offline Find and replace only the last match Windows 10 Find and replace only the last match Office 2016
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 20,738
macropod has a brilliant futuremacropod has a brilliant futuremacropod has a brilliant futuremacropod has a brilliant futuremacropod has a brilliant futuremacropod has a brilliant futuremacropod has a brilliant futuremacropod has a brilliant futuremacropod has a brilliant futuremacropod has a brilliant futuremacropod has a brilliant future
Default

A bit of time spent in the VBA help files could have led you to that solution.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
Reply

Thread Tools
Display Modes


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 the last match Find a match and move row to top of page Auto Excel Programming 3 03-08-2016 01:17 AM
Find and replace only the last match 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 the last match Bad view when using Find and Find & Replace - Word places found string on top line paulkaye Word 4 12-06-2011 11:05 PM

Other Forums: Access Forums - Senior Forums

All times are GMT -7. The time now is 09:18 AM.


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