Help with Repeated Find and Select in a Specified Table Range (Col

D

dejones923

I have an established range in a table. In that range there is verbiage
surrounded by parenthesis like (abc) (defghi) (jklm) etc. I would like to
select each instance in the range to do something with it before going to the
next row.

Can anyone help with this.

Sub GetVerbiage()

Dim totCount As Integer, i As Integer, myVerbiage As String,
rngToSearch As Range, rngResult As Range, intRow As Integer

totCount = ActiveDocument.Tables(23).Rows.Count
For i = 14 To totCount
Set rngToSearch =
ActiveDocument.Tables(23).Rows(i).Cells(7).Range
Set rngResult = rngToSearch.Duplicate
With rngResult.Find
.ClearFormatting
.Text = "\(*\)"
.Forward = True
.MatchWildcards = True
.Wrap = wdFindContinue
.Execute
End With

If rngResult.Find.Found Then
'do something here
End If

'At this point there is other verbiage in the range that I
would like to look at and maybe do something with it but the code jumps
to the next i. How do I get it to look at the next instance where there is
verbiage surronded by parenthesis like (Now is the time1) (Now is the time2)'

Next i
End Sub
 
J

Jay Freedman

Replace these lines

.Execute
End With

If rngResult.Find.Found Then
'do something here
End If

with these lines

Do While .Execute And rngResult.InRange(rngToSearch)
'do something here
Loop
End With

The .Execute method returns a boolean result that is always exactly the same
as the value of rngResult.Find.Found. A side effect of the .Execute is that
rngResult is redefined to cover only the found text. The InRange method
checks that the found text is within rngToSearch, so the loop stops after
processing the last occurrence within the search range.

If the processing that occurs inside the loop changes the size or contents
of rngResult, then the last statement before the Loop line must set
rngResult to the end of its original location, so the next iteration starts
searching from that point. Failure to do this can result in an infinite loop
if it causes the search to find the same location again. If all you're doing
is reformatting or copying the range, this step isn't necessary.

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the newsgroup so
all may benefit.
 

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