Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / July 2007

Tip: Looking for answers? Try searching our database.

Find/Replace: Find numbers and replace with n-1

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Alex Wenzel - 25 Jul 2007 14:12 GMT
Hi,

this is not strictly a VBA question (rather about the advanced find/
replace feature), but close:

I'm trying to find out how to find certain numbers inside specific
text strings, modify the text string and replace each number with n-1
(i.e. make >abc=1< to #xyz=0#, >abc=2< to #xyz=1# etc.).
I know how to modify the text string while *preserving* the numbers,
but have no idea how to calculate the n-1 number in the replacement
string.

So I would search for something like \>abc\=([0-9]{1,})\<
and replace it with .... what?

Is there a way to do this w/ find/replace or do I need a full VBA
macro?

Any ideas?
TIA
periodic - 25 Jul 2007 14:46 GMT
I don't you can do that with a pure find replace. The problem is that regular
expressions are too limited for operations like that. They deal with strings
and replacing strings, they can not modify what they find in that sens. You
have to use some VBA to fix this. Try recording a find replace in a macro and
go from that.

Of course you can do several search and replace and take each number by
itself.

> Hi,
>
[quoted text clipped - 16 lines]
> Any ideas?
> TIA
Helmut Weber - 27 Jul 2007 12:16 GMT
Hi Alex Wenzel,

>I'm trying to find out how to find certain numbers inside specific
>text strings, modify the text string and replace each number with n-1
>(i.e. make >abc=1< to #xyz=0#, >abc=2< to #xyz=1# etc.).

>Is there a way to do this w/ find/replace
I don't think so.
>or do I need a full VBA macro?

Very probably.

>Any ideas?

Yes, have a close look at this one:

Sub Test6009()
Dim rDcmn As Range     ' range document
Dim sTmp1 As String    ' temporary string 1
Dim sTmp2 As String    ' temporary string 2
Dim lPstn As Long      ' position in a string
Set rDcmn = ActiveDocument.Range
With rDcmn.Find
  .Text = "\>abc\=([0-9]{1,})\<"
  .MatchWildcards = True
  While .Execute
     sTmp2 = ""
     ' rDcmn.Select
     sTmp1 = rDcmn.Text
     sTmp1 = "#" & Right(sTmp1, Len(sTmp1) - 1)
     lPstn = InStr(sTmp1, "=") + 1
     While IsNumeric(Mid(sTmp1, lPstn, 1))
        sTmp2 = sTmp2 & Mid(sTmp1, lPstn, 1)
        lPstn = lPstn + 1
     Wend
     sTmp2 = CStr(CLng(sTmp2))
     lPstn = InStr(sTmp1, "=")
     sTmp1 = Left(sTmp1, lPstn) & sTmp2 & "#"
     ' msgbox sTmp1
     rDcmn.Text = sTmp1
     rDcmn.Start = rDcmn.End
     rDcmn.End = ActiveDocument.Range.End
  Wend
End With
End Sub

Improvably in many ways.

HTH

Signature

Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

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

Alex Wenzel - 30 Jul 2007 00:09 GMT
Thanks, Helmut.

I missed your reply because I had to get it done quickly and had
already done it manually.
Will try it next time the problem arises.

Alex

Rate this thread:






 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.