View Full Version : Change Number in in line IF...
Good Guy
01-22-2016, 02:33 PM
Hello
i need your help.
I have document with about 600 lines
Example of lines:
G08 /fly escluso
G90 /assoluto
OB14 = 1 /apri pinza
N4 IF IB14 = 0 GO 4 /attesa pinza aperta
OB14 = 0
G00 X-54.412 Y-1722.779 Z-0.002 A-0.002 /movimento
N7 IF IB15 = 0 GO 7
OB31 = 1 /selezione senso di rotazione
OB14 = 1 /apri pinza
G04 F500 /ritardo
N11 IF IB14 = 0 GO 11 /attesa pinza aperta
OB14 = 0
OB30 = 1 /inizio scambio tavola
G04 F1000 /ritardo
N15 IF IB07 = 1 GO 15 /tavola in battuta
G04 F1000 /ritardo
OB30 = 0 /fine scambio tavola
OB15 = 1 /annull. consenso scambio tavola
G04 F500 /ritardo
OB15 = 0
G00 X10.327 Y-1597.015 Z-0.020 A-0.020 /movimento
OB13 = 1 /chiudi pinza
N23 IF IB13 = 0 GO 23 /attesa pinza chiusa
for understanding let's check the lines
line number 20 OB15 = 0
line number 21 G00 X10.327 Y-1597.015 Z-0.020 A-0.020 /movimento
line number 22 OB13 = 1 /chiudi pinza
line number 23 N23 IF IB13 = 0 GO 23 /attesa pinza chiusa
the number after letter N must be the same as the line number.
What i need is VBA code which will check if the number after the letter N is the same as the line number. If it's not then should change it to the correct value. So if N23 is in line number 45 then should change it to N45.
Everything other data is not important and should not be changed.
If there is someone who can help me with this, i would be really thankful.
Thanks
gmayor
01-23-2016, 02:14 AM
It depends what you mean by Line Numbers. There are no 'lines' in a Word document. If the 'lines' correspond to the paragraphs in the document then the following should work. Word's built in line numbering, which is not part of the document text, has a potentially different count and restarts each page, despite the display options for line numbering. You have not indicated how empty paragraphs, if present, should be handled.
Option Explicit
Sub CheckLineNumbers()
Dim oPara As Paragraph
Dim oRng As Range
Dim i As Long
i = 1
For Each oPara In ActiveDocument.Paragraphs
If oPara.Range.Characters(1) = "N" Then
Set oRng = oPara.Range
oRng.Start = oRng.Start + 1
oRng.Collapse 1
oRng.MoveEndWhile "0123456789"
oRng.Text = i
End If
i = i + 1
Next oPara
lbl_Exit:
Exit Sub
End Sub
Good Guy
01-24-2016, 01:49 PM
It depends what you mean by Line Numbers. There are no 'lines' in a Word document. If the 'lines' correspond to the paragraphs in the document then the following should work. Word's built in line numbering, which is not part of the document text, has a potentially different count and restarts each page, despite the display options for line numbering. You have not indicated how empty paragraphs, if present, should be handled.
Option Explicit
Sub CheckLineNumbers()
Dim oPara As Paragraph
Dim oRng As Range
Dim i As Long
i = 1
For Each oPara In ActiveDocument.Paragraphs
If oPara.Range.Characters(1) = "N" Then
Set oRng = oPara.Range
oRng.Start = oRng.Start + 1
oRng.Collapse 1
oRng.MoveEndWhile "0123456789"
oRng.Text = i
End If
i = i + 1
Next oPara
lbl_Exit:
Exit Sub
End Sub
I would like to thank you!
your code works just like I wanted.
Thanks again
Good Guy
01-26-2016, 11:12 AM
OK so I have another question.
in some lines I have "GO"
N501 IF IB16 = 0 GO 475 /attesa saldatura completa
OB16 = 0
N503 IF (IB11 = 0) OR (IB12 = 0) GO 477
G00 X-373.696 Y-784.820 Z-0.002 A-0.002 /movimento
What I would like to do is when N numbers are changed, the same must be done after GO
for example if we look code above
N501 IF IB16 = 0 GO 501
OB16 = 0
N503 IF (IB11 = 0) OR (IB12 = 0) GO 503
G00 X-373.696 Y-784.820 Z-0.002 A-0.002 /movimento
so when N number is changed the same must be done for GO (N and GO are in the same line)
If this is possible i would like to know how.
Thanks in advanced!
gmayor
01-26-2016, 11:32 PM
It is possible with the same proviso as before, and another.
Your example shows that not only does the number after GO change, but you have added Bold tags, not mentioned in the text of your message. The following will change the number following GO, but will not add the tags. If you want the additional tags, then change the line
oFind.Text = i
to
oFind.Text = "" & i & ""
However it will add the tags to all instances after GO
Option Explicit
Sub CheckLineNumbers()
Dim oPara As Paragraph
Dim oRng As Range, oFind As Range
Dim i As Long
i = 1
For Each oPara In ActiveDocument.Paragraphs
If oPara.Range.Characters(1) = "N" Then
Set oRng = oPara.Range
oRng.Start = oRng.Start + 1
oRng.Collapse 1
oRng.MoveEndWhile "0123456789"
Set oFind = oRng.Paragraphs(1).Range
oFind.End = oFind.End - 1
With oFind.Find
Do While .Execute(FindText:="GO", MatchCase:=True)
oFind.MoveStartUntil "0123456789"
oFind.MoveEndWhile "0123456789"
oFind.Text = i
Exit Do
Loop
End With
oRng.Text = i
oRng.Collapse 0
End If
i = i + 1
Next oPara
lbl_Exit:
Set oRng = Nothing
Set oFind = Nothing
Set oPara = Nothing
Exit Sub
End Sub
Good Guy
01-27-2016, 12:34 PM
Sorry for bold text. bold was only for marking GO text
An your code works perfect.
You have saved me a lot of time!
Thanks
Good Guy
07-19-2016, 11:15 AM
I am back ;)
i need to upgrade the code you guys created with some more.
what i need is use the same code in same document
I have textbox in word in which i type a number and that number needs to be replaced with all numbers which come after line which starts with G00 X...
Example: TEXTBOX=99
G00 X-54.412 Y-1722.779 Z-0.002 A-0.002
will then be
G00 X99 Y-1722.779 Z-0.002 A-0.002
every others number must not change
Numbers can be negative or positive(- sign after X is minus)
If you guys can help me with this problem I will be very happy
Thanks again!
gmaxey
07-19-2016, 01:58 PM
Here is one way:
Sub Test()
ScratchMacro "99" 'Pass the value of your textbox.
End Sub
Sub ScratchMacro(lngPassed As Double)
Dim oRng As Word.Range
Dim oRngW As Word.Range
Set oRng = ActiveDocument.Range
With oRng.Find
.Text = "G00 X"
While .Execute
oRng.Collapse wdCollapseEnd
Do Until oRng.Characters.Last.Next = " "
oRng.MoveEnd wdCharacter, 1
Loop
oRng.Text = lngPassed
oRng.Collapse wdCollapseEnd
Wend
End With
End Sub
Good Guy
07-20-2016, 12:03 PM
Thank you sir, you are a legend
gmaxey
07-20-2016, 01:20 PM
A legend? Ha! You're welcome. Glad to help.
Powered by vBulletin® Version 4.2.5 Copyright © 2025 vBulletin Solutions Inc. All rights reserved.