Add [] around numbers

  • Thread starter Thread starter Jason
  • Start date Start date
J

Jason

Hi all,

I have a Word document and there are a lot of numbers in
it. I want to use Find and Replace to add [] around each
of them, for example 12 --> [12] or 12.5 --> [12.5]. How
should I do it?

Thanks a lot.
 
See the article "Finding and replacing characters using wildcards" at:

http://word.mvps.org/FAQs/General/UsingWildcards.htm

To deal with both decimal and not decimal numbers, and whether or not the
non-decimal numbers are in a paragraph by themselves or have a space before
and after them, you will need to use a macro such as the following:

Dim myrange As Range
Selection.HomeKey wdStory
Selection.Find.ClearFormatting
With Selection.Find
Do While .Execute(FindText:="[0-9]{1,}.[0-9]{1,}", MatchWildcards:=True,
Wrap:=wdFindStop, Forward:=True) = True
Selection.Range.InsertBefore "["
Selection.Range.InsertAfter "]"
Selection.Collapse wdCollapseEnd
Loop
End With
Selection.HomeKey wdStory
Selection.Find.ClearFormatting
With Selection.Find
Do While .Execute(FindText:="^13[0-9]{1,}^13", MatchWildcards:=True,
Wrap:=wdFindStop, Forward:=True) = True
Set myrange = Selection.Range
myrange.Start = myrange.Start + 1
myrange.End = myrange.End - 1
myrange.InsertBefore "["
myrange.InsertAfter "]"
Selection.Collapse wdCollapseEnd
Loop
End With
Selection.HomeKey wdStory
Selection.Find.ClearFormatting
With Selection.Find
Do While .Execute(FindText:="^13[0-9]{1,} ", MatchWildcards:=True,
Wrap:=wdFindStop, Forward:=True) = True
Set myrange = Selection.Range
myrange.Start = myrange.Start + 1
myrange.End = myrange.End - 1
myrange.InsertBefore "["
myrange.InsertAfter "]"
Selection.Collapse wdCollapseEnd
Loop
End With
Selection.HomeKey wdStory
Selection.Find.ClearFormatting
With Selection.Find
Do While .Execute(FindText:=" [0-9]{1,} ", MatchWildcards:=True,
Wrap:=wdFindStop, Forward:=True) = True
Set myrange = Selection.Range
myrange.Start = myrange.Start + 1
myrange.End = myrange.End - 1
myrange.InsertBefore "["
myrange.InsertAfter "]"
Selection.Collapse wdCollapseEnd
Loop
End With


And, now I realise that there is another possibility that I have not covered
which is a number that is the first thing in a new paragraph and that is
followed by a tab, but I trust that I have shown you enough to modify it to
catch that situation as well if necessary.


--
Please respond to the Newsgroup for the benefit of others who may be
interested. Questions sent directly to me will only be answered on a paid
consulting basis.

Hope this helps,
Doug Robbins - Word MVP
 
Back
Top