Macro to find, edit, and replace text

B

Brian

I have attempted to find a macro for find/replace on this forum but I can't
find one that does exactly what I want and when I try to edit one thats is
close I can't seem to get it to work.

My problem: I need to search a word doc for any 3 digit number, take that
number subtract 537 and replace the old number with the new result. The
numbers in the doc are in no particular order and may appear between 1-5
times each through the text.

Please HELP!
 
D

Doug Robbins - Word MVP on news.microsoft.com

Use

Selection.HomeKey wdStory
Selection.Find.ClearFormatting
Dim frange As Range
With Selection.Find
Do While .Execute(FindText:="[0-9]{3}", Forward:=True, _
MatchWildcards:=True, Wrap:=wdFindStop, MatchCase:=False) = True
Set frange = Selection.Range
Selection.Start = frange.End + 1
frange.Text = Val(frange.Text) - 537
Loop
End With


--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP, originally posted via msnews.microsoft.com
 
B

Brian

Thank you so much, It works great, and yet so simple.
Brian


Doug Robbins - Word MVP on news.microsof said:
Use

Selection.HomeKey wdStory
Selection.Find.ClearFormatting
Dim frange As Range
With Selection.Find
Do While .Execute(FindText:="[0-9]{3}", Forward:=True, _
MatchWildcards:=True, Wrap:=wdFindStop, MatchCase:=False) = True
Set frange = Selection.Range
Selection.Start = frange.End + 1
frange.Text = Val(frange.Text) - 537
Loop
End With


--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP, originally posted via msnews.microsoft.com

Brian said:
I have attempted to find a macro for find/replace on this forum but I can't
find one that does exactly what I want and when I try to edit one thats is
close I can't seem to get it to work.

My problem: I need to search a word doc for any 3 digit number, take that
number subtract 537 and replace the old number with the new result. The
numbers in the doc are in no particular order and may appear between 1-5
times each through the text.

Please HELP!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top