#1
|
|||
|
|||
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! |
#2
|
|||
|
|||
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 |
#3
|
||||
|
||||
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] |
#4
|
|||
|
|||
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.
|
#5
|
|||
|
|||
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?
|
#6
|
|||
|
|||
Moving ahead
I've gone ahead and just created sub functions for each new string value. I think this will work now.
|
#7
|
||||
|
||||
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] |
|
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 |
Problem replacing text. | piritzo | Word | 2 | 06-22-2013 12:50 AM |
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 |