PDA

View Full Version : Macro for word 2003



Lio
11-29-2011, 06:16 AM
Hi

I am looking for a macro in which it finds 2 numbers(any length or digits)
and replace with  in between them

The numbers should be in increasing order (only) like 22-23 or 123-134 or 1-4
so the out put as required is

22(#&23)23
123(#&23)134

etc

Talis
12-02-2011, 12:20 PM
With all due respect may I suggest you start a fresh thread with a similar request for help giving the thread a more indicative name. The current one is plainly silly for a forum that deals exclusively with Word VBA.
An example could be "Find and modify a numerical sequence and add a control code". The reason for an indicative title is so that relevant threads can be located easily by others when looking through past threads.

You are more likely to get assistance if you give an indication of the broader picture of what you are trying to do. In this case, are you trying to insert a display or printing control code for a non-English language?

In your examples there is a square. Why is that there?
You give examples which are confusing. What is the relationship between the 23 in 22-23 and the 23 in (#&23)? In one case one is shown before the other and in the next case the opposite. Similarly the box is both before and after (#&23). Which is correct? Is there always a space after the numerical sequence? It makes a difference in the VBA if the termination of the sequence is known in advance (although it can be worked round).

If you looked through past threads you would find that the experts (not me) help those who have made some attempt to create a subroutine - even if it is only a recorded macro based on Find/Replace and have put their code in the thread. This gives a much better indication of what is being attempted and the expert won't be wasting his/her time as a result of misinterpreting an imprecise request. It also indicates that the poster is prepared to make some effort such as creating the 'Find' expression.

Yes, I have written a subroutine that will do what I interpret your request to mean. I'll post it later.

Lio
12-02-2011, 01:11 PM
OK, I will reply as it is:


In your examples there is a square. Why is that there?

Reply: It is not supposed to be a square it got displayed after the actual query got posted.

See in example like 22-33

there are numbers in increasing order(increasing order is a must) and a hyphen between them and the numbers could be of any digits (eg. 222-334 or 1345-1789 etc)

Now what is required here is to find such sequence where hyphen is in between them, and then replace the hyphen only with the corresponding decimal code between the numbers

eg. say xxx is the hyphen's decimal code so in the sequence 22-33
it should be 22xxx33

I think it will explain my query better
--------------------------

This is my first or second query only and from next time I will mark it as said, which will give clear view too

Talis
12-03-2011, 11:11 AM
I'm even more confused now!:banghead:


replace the hyphen only with the corresponding decimal code between the numbers
What does "the corresponding decimal code" mean? Corresponding to what? Should it always be #&23 ?

Let's say the number sequence is 1345-1789.
What should the modified sequence be?


It may be best to put your answer in Code tags like this.

You do this by putting open square bracket CODE close square bracket before the text, then open square bracket /CODE close square bracket after the text.
This should avoid the square (representing a non-displayable character) appearing.

Here is a subroutine which is based on the assumption that you always want #&23 in round brackets to replace the hyphen.

Sub ModNumSeq()
Dim oRng As Word.Range
Dim strTmp As String

Const conStr As String = "(#&23)" '***** Change the (#&23) to your needs *****

Set oRng = ActiveDocument.Range
With oRng.Find
.MatchWildcards = True
.Text = "[0-9]@-[0-9]{1,10}"
While .Execute
With oRng
strTmp = oRng
If Val(Left(strTmp, InStr(strTmp, "-") - 1)) < Val(Right(strTmp, InStrRev(strTmp, "-") - 1)) Then
.Delete
.InsertAfter Left(strTmp, InStr(strTmp, "-") - 1) & conStr & Right(strTmp, InStrRev(strTmp, "-") - 1)
End If
End With
oRng.Collapse wdCollapseEnd
Wend
End With
End Sub

Lio
12-03-2011, 03:07 PM
Thanks for all your efforts its working

Corresponding decimal code means −(for - or hyphen) in actual,

I will try to get into this first and comes up with some more Q's with you.

Thanks again.

macropod
12-04-2011, 08:26 PM
Hi talis,

Minor code enhancement:
Sub ModNumSeq()
Dim oRng As Range, strTmp As String
Const conStr As String = "(#&23)" '***** Change the (#&23) to your needs *****
Set oRng = ActiveDocument.Range
With oRng.Find
.MatchWildcards = True
.Text = "<[0-9,.]{1,10}-[0-9,.]{1,10}>"
While .Execute
With oRng
strTmp = .Text
If Split(strTmp, "-")(0) < Split(strTmp, "-")(1) Then
.Text = Val(Split(strTmp, "-")(0)) & conStr & Val(Split(strTmp, "-")(1))
.Collapse wdCollapseEnd
End If
End With
Wend
End With
End Sub
Note Split function and provision for decimal numbers & thousands separators.