Find a number and replace it with that number less one?

S

Smokin

Please help. I'm trying to change a data list in Word 2002 that has
the following format:

[Bank 112]
2=BrightPiano Natural!
4=Honky Tonk Regular
6=DX Modern Regular
7=Harpsichord Regular
9=Celesta Regular
108=Glocken Regular

Each of the numbers that are followed by an "=" needs to be replaced
by itself minus 1, so that the above list would become:

[Bank 112]
1=BrightPiano Natural!
3=Honky Tonk Regular
5=DX Modern Regular
6=Harpsichord Regular
8=Celesta Regular
107=Glocken Regular

These items were originally listed as 1-based, and need to be
transformed to a 0-based list. There are several banks of not
sequentially numbered entries. Is there a macro, vba, or search and
replace trick that will save me the labor of manually re-typing my
entire (very long) list?


Thank you for any help!
 
T

Tony Jollans

I don't think you can do it directly with a Find and Replace but this code
should do it for you ...

Selection.HomeKey wdStory

Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "<[0-9]{1,}="
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindStop
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = True
End With

While Selection.Find.Execute
Selection.Text = Str(Val(Selection) - 1) & "="
Selection.Collapse wdCollapseEnd
Wend
 
S

Smokin

Thank you very much... that did the math just fine. It leaves a
single space at the begining of the line. (maybe for a +/- sign?) .
Is there a way to now truncate the line and get rid of that beginning
space? Then all wil be well.

Thanks for your help.
 
H

Helmut Weber

Hi,

try CStr instead of Str.

--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Win XP, Office 2003
"red.sys" & Chr$(64) & "t-online.de"
 

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