a Loop that inserts

S

Steved

Hello from Steved

I’m looking for a loop to look for
L [0]:[0]:[0]
R [0]:[0]:[0]
If the above is not found then insert

The below is 4 paragraphs so the below is ok so ignore

Prz
L 10:3:1
R 8:1:3
F 1:0:0

It may find L [0]:[0]:[0] missing as shown below so insert L [0]:[0]:[0]
Or R [0]:[0]:[0] missing so insert R [0]:[0]:[0]
In some cases it may need to insert both L [0]:[0]:[0], R [0]:[0]:[0]


Prz
R 8:1:3
F 1:0:0

In finishing, Prz is the start and F [0]:[0]:[0] is the end

Thankyou
 
D

David Sisson

I hope I understood correctly.

From your example, I created data like this

Prz
L 10:3:1
R 8:1:3
F 1:0:0

Prz
R 8:1:3
F 1:0:0

Prz
L 10:3:1
F 1:0:0

Prz
F 1:0:0

Prz
L 10:3:1
R 8:1:3
F 1:0:0

and the code changes it to this:

Prz
L 10:3:1
R 8:1:3
F 1:0:0

Prz
L 0:0:0
R 8:1:3
F 1:0:0

Prz
L 10:3:1
R 0:0:0
F 1:0:0

Prz
L 0:0:0
R 0:0:0
F 1:0:0

Prz
L 10:3:1
R 8:1:3
F 1:0:0

Hope this helps!

Sub Search4MissingParts()

Dim aDoc As Document
Dim SearchRng As Range
Dim AllRng As Range

Set aDoc = ActiveDocument

Set AllRng = ActiveDocument.Range
Set SearchRng = AllRng.Duplicate

Do
With SearchRng.Find
.ClearFormatting
.Text = "PRZ"
.Forward = True
.Wrap = wdFindStop
.Execute
End With

If Not SearchRng.Find.Found Then Exit Do

SearchRng.MoveStart wdParagraph, 1
SearchRng.MoveEnd wdParagraph, 1
'SearchRng.Select
If Left(SearchRng, 1) = "F" Then
SearchRng.InsertBefore "L 0:0:0" & vbCr & "R 0:0:0" & vbCr
Else
If Left(SearchRng, 1) <> "L" Then
SearchRng.InsertBefore "L 0:0:0" & vbCr
SearchRng.Collapse wdCollapseStart
End If

SearchRng.MoveStart wdParagraph, 1
SearchRng.MoveEnd wdParagraph, 1
'SearchRng.Select
If Left(SearchRng, 1) <> "R" Then
SearchRng.InsertBefore "R 0:0:0" & vbCr
End If

End If
'SearchRng.Select
'Stop
SearchRng.End = AllRng.End
Loop Until Not SearchRng.Find.Found

End Sub
 
S

Steved

Hello David from Steve Dee

Thankyou very much

the key was I did not Know how to insert before "F"

have a good day.
 
S

Steved

Hello David from Steved

David is it possible please to have it look from F [0]:[0]:[0] upwards.
"Sorry" as it will then do what I require as show'n in the below. Thankyou.

The below example using your code inserted but the L 0:0:0 and R 0:0:0
however if you look above "F" you will find L 10:3:1 and R 8:1:3 so in this
case I needed it to ignore.


Prz $28,150
L 0:0:0
R 0:0:0
Win 22%
Plc 44%
SPR $1563
Days 12
L 10:3:1
R 8:1:3
F 1:0:0

It should be

Prz $28,150
Win 22%
Plc 44%
SPR $1563
Days 12
L 10:3:1
R 8:1:3
F 1:0:0
 
D

David Sisson

I'm still confused on what conditions warrant correction.

Could you provide a few examples of all possible situations where the
data would need to be changed/modified.?

In the last example, a simple search/replace from the keyboard would
correct that data. I can only guess that other conditions apply that
prevent that approach.
 
S

Steved

Hello David from Steved

David the objective is to insert only if L [0]:[0]:[0] or R [0]:[0]:[0] or
somecases both are not found, if for example L 10:3:1 and R 8:1:3 are found
then do nothing, goto next.

example below is ok so do nothing

Prz $28,150
Win 22%
Plc 44%
SPR $1563
Days 12
L 10:3:1
R 8:1:3
F 1:0:0

The below has no L [0]:[0]:[0] or R [0]:[0]:[0] so above F 1:0:0 isert L
[0]:[0]:[0] and R [0]:[0]:[0]

Prz $28,150 To This Prz $28,150
Win 22% Win 22%
Plc 44% Plc 44%
SPR $1563 SPR $1563
Days 12 Days 12
F 1:0:0 L 0:0:0
R 0;0:0
F 1:0:0


another example the below has no L [0]:[0]:[0] so insert above R 8:1:3

Prz $28,150 To this Prz $28,150
Win 22% Win 22%
Plc 44% Plc 44%
SPR $1563 SPR $1563
Days 12 Days 12
R 8:1:3 L 0;0:0
F 1:0:0 R 8:1:3
F 1:0:0

another example the below has no R [0]:[0]:[0] so insert above F [0]:[0]:[0]

Prz $28,150 To this Prz $28,150
Win 22% Win 22%
Plc 44% Plc 44%
SPR $1563 SPR $1563
Days 12 Days 12
L 19:1:3 L 19:1:3
F 1:0:0 R 0:0:0
F 1:0:0

Finally David your code only inserts if not found please.

It does not Replace if found it just moves to the next as show'n in the
above examples.

Thankyou for taking timeout.
 
D

David Sisson

Ok try this. Probably not the best solution, (not as dynamic as I
like) but I am recovering from the flu and can't quite think straight
yet. (That's my excuse and I'm sticking with it. :)

Sub Main1()

Dim aDoc As Document
Dim SearchRng As Range
Dim AllRng As Range

Set aDoc = ActiveDocument

Set AllRng = ActiveDocument.Range
Set SearchRng = AllRng.Duplicate

Do
With SearchRng.Find
.ClearFormatting
.Text = "F 1:0:0"
.Forward = True
.Wrap = wdFindStop
.Execute
End With

If Not SearchRng.Find.Found Then Exit Do

SearchRng.MoveStart wdParagraph, -1
SearchRng.MoveEnd wdParagraph, -1
SearchRng.Select
'Stop
If Left(SearchRng, 1) = "D" Then
SearchRng.InsertAfter "L 0:0:0" & vbCr & "R 0:0:0" & vbCr
SearchRng.MoveStart wdParagraph, 4
Else
If Left(SearchRng, 1) = "L" Then
SearchRng.InsertAfter "R 0:0:0" & vbCr
SearchRng.MoveStart wdParagraph, 3
Else
If Left(SearchRng, 1) = "R" Then
SearchRng.InsertBefore "L 0:0:0" & vbCr
SearchRng.MoveStart wdParagraph, 3
End If
End If
End If
SearchRng.End = AllRng.End
Loop Until Not SearchRng.Find.Found

End Sub
 
S

Steved

Hello David from Steved

Glad to hear you are getting over the Flu.

Thanyou I've had a run with your code and I'm very happy, there is still one
or two situations but in the main you have given me something to work with.
 

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