Find and Fix All

T

ThomasJColby

Word2000; I want code to find each series of 6 digits and
add a - between digits 5 and 6 for example 606380412 would
become 60638-0412

I can make this happen 1 time, 10 times, and so on because
I do not know how to make it simply find and fix all. Here
is the code I created to find and fix 1 occurence. Thanks
for your help!!!

Selection.Find.ClearFormatting
With Selection.Find
.Text = "^#^#^#^#^#^#^#^#^#"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute
Selection.EndKey Unit:=wdLine
Selection.MoveLeft Unit:=wdCharacter, Count:=4
Selection.TypeText Text:="-"
End Sub
 
D

Dave Lett

Hi Thomas

You _might_ be able to use a wildard search and replace

With Selection.Fin
.Text = "([0-9]{5,5})
.ClearFormattin
.MatchWildcards = Tru
With .Replacemen
.Text = "\1-
.ClearFormattin
End Wit
.Execute Replace:=wdReplaceAl
End Wit

If, however, the remaining digits are 5 or more, then it will replace there, too. For example, 60638041222 would become 60638-04122-2

If you have these longer numbers, then you can use the following

With Selection.Fin
.Text = "([0-9]{5,})
.ClearFormattin
.MatchWildcards = Tru
Do While .Execut
Selection.Characters(5).InsertAfter "-
Selection.MoveRigh
Loo
End Wit
HTH
Dave
 
K

Klaus Linke

If you have these longer numbers, then you can use the following:


You could also try the wildcard replacement
Find what: (<[0-9]{6})([0-9])
Replace with: \1-\2

The "word anchor" < makes sure you don't match digits in the middle of a
number, and the second group ([0-9]) assures that you only insert a hyphen
if there are more numbers than 6.

Regards,
Klaus
 
T

ThomasjColby

Gentlemen,

This looked sooo promising, I should give more information
however. This is to fix zipcodes where there is 9 digits
only. So, this must only add a dash when it finds 9
digits. Here is the problem that I ran into with Dave's
code:

What was; 15434 Chicago Street
Is now; 15434- Chicago Street
-----Original Message-----
Hi Thomas,

You _might_ be able to use a wildard search and replace.

With Selection.Find
.Text = "([0-9]{5,5})"
.ClearFormatting
.MatchWildcards = True
With .Replacement
.Text = "\1-"
.ClearFormatting
End With
.Execute Replace:=wdReplaceAll
End With

If, however, the remaining digits are 5 or more, then it
will replace there, too. For example, 60638041222 would
become 60638-04122-2.
If you have these longer numbers, then you can use the following:

With Selection.Find
.Text = "([0-9]{5,})"
.ClearFormatting
.MatchWildcards = True
Do While .Execute
Selection.Characters(5).InsertAfter "-"
Selection.MoveRight
Loop
End With
HTH,
Dave
.
 
K

Klaus Linke

I have no idea about ZIP codes and what they can/should look like.

If the problem is just limiting it to 6 to 9 numbers, change the wildcard
search to
Find what: (<[0-9]{5})([0-9]{1,4}>)
Replace with: \1-\2

Klaus
 

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