Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 10-21-2016, 06:28 AM
dwirony dwirony is offline If statement for find and replacing certain text Windows 7 64bit If statement for find and replacing certain text Office 2003
Advanced Beginner
If statement for find and replacing certain text
 
Join Date: Oct 2016
Posts: 49
dwirony will become famous soon enough
Default If statement for find and replacing certain text

Hello all again,



I'm working on another macro that can use an if statement to FindText and then based on finding a certain text, can replace another string within the document. For example, if the string "State of Florida" is found at ANY point in the document, it would replace another string, "STATE TAX" with the corresponding tax info, etc. I'm trying to build an If/ElseIf function to do something like this, could anyone lend a hand?

Much appreciated!
Reply With Quote
  #2  
Old 10-21-2016, 01:49 PM
gmaxey gmaxey is offline If statement for find and replacing certain text Windows 7 32bit If statement for find and replacing certain 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 arrParts() As String
Dim oRng As Word.Range
  arrParts = Split(ActiveDocument.Range.Text, "State of Florida")
  If UBound(arrParts) > 0 Then
    Set oRng = ActiveDocument.Range
    With oRng.Find
      .Text = "STATE TAX"
      .Replacement.Text = "0.00"
      .Execute Replace:=wdReplaceAll
    End With
  End If
lbl_Exit:
  Exit Sub
End Sub
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
  #3  
Old 10-21-2016, 02:09 PM
macropod's Avatar
macropod macropod is offline If statement for find and replacing certain text Windows 7 64bit If statement for find and replacing certain text Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

This might be made rather simpler and less error-prone if you said precisely where in the document these data occur. For example, while Greg's code will work for content located somewhere in the document body, the 'State of Florida' could be anywhere there and every instance of 'STATE TAX' would be replaced, yet it wouldn't work with anything in headers, footers, textboxes, etc.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #4  
Old 10-24-2016, 07:01 AM
dwirony dwirony is offline If statement for find and replacing certain text Windows 7 64bit If statement for find and replacing certain text Office 2003
Advanced Beginner
If statement for find and replacing certain text
 
Join Date: Oct 2016
Posts: 49
dwirony will become famous soon enough
Default Thanks

Thanks for your note Macro. Fortunately, in the documents I generate the State of Florida and "STATE TAX" only generate once and in one location. So it shouldn't be an issue in replacing other strings of text.
Reply With Quote
  #5  
Old 10-24-2016, 07:11 AM
dwirony dwirony is offline If statement for find and replacing certain text Windows 7 64bit If statement for find and replacing certain text Office 2003
Advanced Beginner
If statement for find and replacing certain text
 
Join Date: Oct 2016
Posts: 49
dwirony will become famous soon enough
Default

Thank you for this, gmaxey! It works perfectly. I'm going to use this to create a lotttt of contingency factors for my documents. Instead of just rerunning this whole code over and over, can I just repeatedly use With oRng.Find after End With each time within the function?
Reply With Quote
  #6  
Old 10-24-2016, 08:29 AM
dwirony dwirony is offline If statement for find and replacing certain text Windows 7 64bit If statement for find and replacing certain text Office 2003
Advanced Beginner
If statement for find and replacing certain text
 
Join Date: Oct 2016
Posts: 49
dwirony will become famous soon enough
Default Moving ahead

I've gone ahead and just created sub functions for each new string value. I think this will work now.
Reply With Quote
  #7  
Old 10-24-2016, 04:03 PM
macropod's Avatar
macropod macropod is offline If statement for find and replacing certain text Windows 7 64bit If statement for find and replacing certain text Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

A single macro could probably have been used to do all the work but, since you never gave the full details, no one could help with that.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
VBA Word - Find & Apply Styles to Specific Words - Using Case Statement jc491 Word VBA 17 12-26-2015 12:25 PM
Replacing the 2nd or 3rd instance of Text wdillon2 Word VBA 2 05-13-2015 10:21 PM
If statement for find and replacing certain text Problem replacing text. piritzo Word 2 06-22-2013 12:50 AM
If statement for find and replacing certain text Help with replacing text with wildcards sbatson5 Word 2 04-13-2012 03:49 AM
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 11:58 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