Not sure how to set up loop conditions

E

Ed

I have reports which reference technical documents scattered in no
easily-used pattern over many tables (150-200 references in 20-35 tables is
typical). Every now and then (like yesterday and today!), I get to take a
list (a Word table) of all the document numbers and make sure they are the
same as what's in the report.

So I'm trying to set up a macro that will loop through my comprehensive
table and check the report to see if that text string is found. I'm having
difficulty setting up the loop conditions, though. The text string, if it
is found, must be the only text in a cell in Column 1 of a table to be a
valid match for my purposes. If it doesn't meet all this, then I need to
Find Next until the whole document is searched and it is or is not found.
Then I increment down the comprehensive table to find the next text string
and do it all over again.

I can get my mind around almost all of it except the loop conditions. Can
someone point me in the right directions?

TIA
Ed
 
M

Mark Tangard

Ed,

Assuming your comprehensive doc (the master with the correct numbers)
contains exactly one single-column table -- that is, each row has one
cell and each cell contains a number to be searched for in the report,
and also assuming the first match found completes the search, i.e.,
we're not dealing with possible recurrence of a number, the macro
below should do it. The macro assumes you begin with the master file
already open and active. You don't say what you want done with the
happy cells vs. the evil cells, so I'm having it color the verified
cells yellow in the master file.

Sub CheckMyDocNumbers()

Dim oDoc As Document, oMaster As Document, oMasterTable As Table
Dim oTbl As Table, oRow As Row, oMasterCell As Cell

Set oMaster = ActiveDocument
Set oMasterTable = oMaster.Tables(1)
Set oDoc = Documents.Open("C:\Path\ReportName.doc") ' <---Substitute

For Each oMasterCell In oMasterTable.Range.Cells
For Each oTbl In oDoc.Tables
For Each oRow In oTbl.Rows
If oRow.Cells(1).Range.Text = oMasterCell.Range.Text Then
oMasterCell.Range.Shading.BackgroundPatternColorIndex = wdYellow
GoTo DoneWithThisMasterCell
End If
Next
Next
DoneWithThisMasterCell:
Next

oMaster.Activate
End Sub

One tip: It helps to post the code you're using, even when you haven't
gotten very far; it saves us the time of building the simple parts.
 

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