Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 05-13-2015, 11:24 AM
wdillon2 wdillon2 is offline Replacing the 2nd or 3rd instance of Text Windows 7 64bit Replacing the 2nd or 3rd instance of Text Office 2013
Novice
Replacing the 2nd or 3rd instance of Text
 
Join Date: May 2015
Posts: 1
wdillon2 is on a distinguished road
Default Replacing the 2nd or 3rd instance of Text

Dear Experts,



I have been trying to write some code to help format a template that creates a report. The Report is created from company software and I always have to make little changes such as Changing the Color of font, deleting/replacing text, and reformatting tables to make it look nicer for clients.

The First issue I have encountered is trying to replace text when there are multiple instances of the same Text, but I only want to change the 2nd or 3rd instance. For example:

Pricing report:

The following Pricing report is blah balh blah blah with the QED figures yada yada yada...

Pricing Graph:
(Graph)

So imagine the above is the Pricing Report, lets say I want to change the Word "pricing" in "Pricing Graph" to yellow, but I do not want any other "pricing" words to change.

I know there is wdReplaceAll- which would change all the "pricing" words to yellow, and there is wdReplaceOne - which would just change the First one in "Pricing Report".

Is there a way to skip the first two and go directly to the Third?

Currently my solution is to turn the all yellow and then turn the other two back to white with the wdReplaceOne feature.

Any help would be appreciated.

Regards,

Whitney
Reply With Quote
  #2  
Old 05-13-2015, 06:43 PM
gmaxey gmaxey is offline Replacing the 2nd or 3rd instance of Text Windows 7 32bit Replacing the 2nd or 3rd instance of Text Office 2010 (Version 14.0)
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,429
gmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the rough
Default

Code:
Sub ScratchMacro()
'A basic Word macro coded by Greg Maxey
Dim oRng As Word.Range
Dim lngInstance As Long, lngCount As Long
  lngCount = 0
  Set oRng = Selection.Range 'or ActiveDocument.Range
  With oRng.Find
    .Text = InputBox("Text to find:")
    lngInstance = InputBox("Sequenced number to find:")
    While .Execute
      lngCount = lngCount + 1
      If lngCount = lngInstance Then
        oRng.HighlightColorIndex = wdYellow
        Exit Sub
        oRng.Collapse wdCollapseEnd
      End If
    Wend
  End With
lbl_Exit:
  Exit Sub
End Sub
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
  #3  
Old 05-13-2015, 10:21 PM
gmayor's Avatar
gmayor gmayor is offline Replacing the 2nd or 3rd instance of Text Windows 7 64bit Replacing the 2nd or 3rd instance of Text Office 2010 32bit
Expert
 
Join Date: Aug 2014
Posts: 4,106
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

If you want to mark Pricing in Pricing Graph then you can search for Pricing Graph and mark the first word of the found text e.g.

Code:
Sub Macro1()
'Graham Mayor 14 May 2015
Dim oRng As Range
Set oRng = ActiveDocument.Range        'or Selection.Range
    With oRng.Find
        Do While .Execute(FindText:="Pricing Graph")
            oRng.Words(1).HighlightColorIndex = wdYellow
            Exit Do        'Remove if you want all 'Pricing Graph' entries to be found
        Loop
    End With
lbl_Exit:
    Exit Sub
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
Reply

Tags
formatting text, selective replacement



Similar Threads
Thread Thread Starter Forum Replies Last Post
Replacing the 2nd or 3rd instance of Text Problem replacing text. piritzo Word 2 06-22-2013 12:50 AM
Replacing the 2nd or 3rd instance of Text Creating a plain text content control for every instance of a word or phrase RobsterCraw Word VBA 16 11-20-2012 03:25 PM
Replacing the 2nd or 3rd instance of Text Help with replacing text with wildcards sbatson5 Word 2 04-13-2012 03:49 AM
Highlight text and find next instance DrDOS Word 0 11-15-2010 04:02 PM
Replacing / editting text LisaC Word 0 02-25-2010 03:40 AM

Other Forums: Access Forums

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


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