how to find and flip number by using StrReverse

H

hallel

I would like to use MACRO to search and replace any number in documen
to flip it (to reverse) like 1234 to 4321. How can I use StrReverse ?
Thanks
 
M

Marty

Howdy Hallel,

The StrReverse works fine, although there are a couple of
small problems. For one thing, if you use a "For Each ...
Next" loop to iterate through the Words collection, then
changing the contents of a word will cause the changed
word to re-appear in the next iteration. This sets up an
infinite loop. The second problem is that the ranges in
the Words collection include all trailing spaces. If you
do a simple StrReverse then you will put all the spaces
from the right hand side onto the left.

The following code addresses those issues.

Marty
(as usual, the email address is a bit altered to avoid
spam bots, the actual address should be msn not nsm.)


Option Explicit

Public Sub InvertAllNumbers()

Dim _
blnInverted As Boolean, _
lngLongWord As Long, _
lngNoSpaces As Long, _
rngNext As Range, _
strFollow As String, _
strNumber As String

' Purpose: examines each word in a document to see if
' it is numeric. If the word is numeric,
' then the order of the numerals is reversed
' (123 becomes 321).
' Comment: Changing the text from 123 to 321 affects
' the "For Each ... Next" loop, so that the
' range containing 321 is what appears in
' the next iteration. Left alone, this will
' cause an infinite loop in which 123 is
' changed to 321 and then back to 123. A
' boolean is used to note when a number has
' been reversed. The boolean directs an
' extra "For Each ... Next" loop
' after a reversal.
' Comment: Each range in the Words collection
' includes the space(s) following the
' text (if any). Simply inverting this
' range will cause the following space to be
' flipped over to the leading edge.
' As a result, a double-space
' is produced on the leading edge of the
' inverted number and inverted number
' is run into the following word
' To prevent this, the number is trimmed of
' spaces and a variable, strFollow, is used
' to track following space(s).

blnInverted = False
For Each rngNext In ActiveDocument.Words
If Not blnInverted Then
strNumber = rngNext.Text
If IsNumeric(strNumber) Then
If Right(strNumber, 1) = " " Then
lngLongWord = Len(strNumber)
strNumber = RTrim(strNumber)
lngNoSpaces = _
lngLongWord - Len(strNumber)
strFollow = Space(lngNoSpaces)
Else
strFollow = ""
End If
rngNext.Text = _
StrReverse(strNumber) & strFollow
blnInverted = True
End If
Else
blnInverted = False
End If
Next rngNext

End Sub
 
K

Klaus Linke

Hi Hallel,

You can use a wildcard search to find any numbers, and then use StrReverse
on the matched text:

Selection.Find.ClearFormatting
With Selection.Find
.Text = "[0-9]{2;}"
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchWildcards = True
End With
While Selection.Find.Execute
Selection.Text = StrReverse(Selection.Text)
Wend

(BTW, WordForums.Com sometimes does funny things to plain text messages; I
hope it won't turn any of the code into smileys or formatting...)

Greetings,
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