Options / If... Then not working...

  • Thread starter JulieJinky via OfficeKB.com
  • Start date
J

JulieJinky via OfficeKB.com

Hi all,


This piece of my macro (not pretty, I know) works exactly the way I want it
to...

However when I add the commented out lines, the lower part (the For Loop) of
the code does not seem to execute... I cannot figure out why...

Any help would be greatly appreciated...


Dim strPatrol As String
Dim i As Integer

If optPatrolOne = True Then strPatrol = "Patrol One"
'ActiveDocument.Tables(4).Cell(2, 4).Select
'Selection.TypeText Text:="71"
'ActiveDocument.Tables(7).Cell(2, 4).Select
'Selection.TypeText Text:="71"
'ActiveDocument.Tables(10).Cell(2, 4).Select
'Selection.TypeText Text:="72"
'ActiveDocument.Tables(13).Cell(2, 4).Select
'Selection.TypeText Text:="71"


If optPatrolTwo = True Then strPatrol = "Patrol Two"
'ActiveDocument.Tables(4).Cell(2, 4).Select
'Selection.TypeText Text:="64"
'ActiveDocument.Tables(7).Cell(2, 4).Select
'Selection.TypeText Text:="64"
'ActiveDocument.Tables(10).Cell(2, 4).Select
'Selection.TypeText Text:="64"
'ActiveDocument.Tables(13).Cell(2, 4).Select
'Selection.TypeText Text:="64"


If optPatrolThree = True Then strPatrol = "Patrol Three"
'ActiveDocument.Tables(4).Cell(2, 4).Select
'Selection.TypeText Text:="80"
'ActiveDocument.Tables(7).Cell(2, 4).Select
'Selection.TypeText Text:="80"
'ActiveDocument.Tables(10).Cell(2, 4).Select
'Selection.TypeText Text:="80"
'ActiveDocument.Tables(13).Cell(2, 4).Select
'Selection.TypeText Text:="80"


Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting

With Selection.Find
.Text = strPatrol
.Replacement.Text = ""
.Forward = True
End With

Selection.Find.Execute

Application.ScreenUpdating = False
For i = 1 To 8
With Selection
If .Find.Forward = True Then
.Collapse Direction:=wdCollapseStart
Else
.Collapse Direction:=wdCollapseEnd
End If
.Find.Execute Replace:=wdReplaceOne
If .Find.Forward = True Then
.Collapse Direction:=wdCollapseEnd
Else
.Collapse Direction:=wdCollapseStart
End If
.Find.Execute
End With
Next i
frmPatrolChoice.Hide

Application.ScreenUpdating = True
Selection.HomeKey Unit:=wdStory
 
T

Tony Jollans

My guess would be that changing the Selection changes the result of the
Find - maybe the first Execute fails when looking forward from Tables(13) -
maybe something else, but that's certainly the area to look at.
 
G

Graham Mayor

If the '72' is a typo and should be '71' (in the first table block) then the
following will fill the tables with the appropriate numbers without moving
the selection, so your selection search should work. If the '72' was not a
typo, then you would need to define a second string for the
Tables(10).Cell(2, 4) text.

As it is not clear what that search is actually doing and where, it is
difficult to advise on how to improve that part.

Dim sNum As String
With ActiveDocument
If optPatrolOne = True Then
strPatrol = "Patrol One"
sNum = "71"
End If
If optPatrolTwo = True Then
strPatrol = "Patrol Two"
sNum = "64"
End If
If optPatrolThree = True Then
strPatrol = "Patrol Three"
sNum = "80"
End If
.Tables(4).Cell(2, 4).Range.Text = sNum
.Tables(7).Cell(2, 4).Range.Text = sNum
.Tables(10).Cell(2, 4).Range.Text = sNum
.Tables(13).Cell(2, 4).Range.Text = sNum
End With

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
J

JulieJinky via OfficeKB.com

Mr. Mayor....

Thanks!! That was brilliant... works like a charm...

BTW... the 72 is not a typo... I've been moving things around to try to get
the 72 in there... this is the only instance that all cells are not the same.
.. I think it's my logic that I am having problems with...

I hate to ask, but a push in the right direction...

Thanks again....

J
 
G

Graham Mayor

OK Try the following. If you want each cell to contain a different number
you will have to define two more strings in the same way and apply them to
the appropriate cells.

Dim sNum1 As String
Dim sNum2 As String
With ActiveDocument
If optPatrolOne = True Then
strPatrol = "Patrol One"
sNum1 = "71"
sNum2 = "72"
End If
If optPatrolTwo = True Then
strPatrol = "Patrol Two"
sNum1 = "64"
sNum2 = "64"
End If
If optPatrolThree = True Then
strPatrol = "Patrol Three"
sNum1 = "80"
sNum2 = "80"
End If
.Tables(4).Cell(2, 4).Range.Text = sNum1
.Tables(7).Cell(2, 4).Range.Text = sNum1
.Tables(10).Cell(2, 4).Range.Text = sNum2
.Tables(13).Cell(2, 4).Range.Text = sNum1
End With


--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
J

JulieJinky via OfficeKB.com

Thanks so much... works great...

I have to admit though that I could not see the solution... where I was going
wrong was I wasn't adding the second variables to ALL the options... just
adding it to the first one... geez... noob mistakes...

Thanks so very much...

J
 

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

Similar Threads


Top