I have just been given the very exciting job of comparing Quality Assurance Manuals to codes and standards. My process is first I put the QA manual that I'm reviewing into a 4 column table in word because the code below is great for searching words in any column (Thanks Greg). Secondly I have to verify the inclusion of the applicable code clauses. I generally pick a key word from the clause and run a search but since the manuals are anywhere from 50 to 200 pages this usually gives way too many returns. I'm wondering if there is a way to enter a sentence or clause I'm looking for into the "input box" and have the macro search for any 3 or 4 consecutive words from that sentence. Is this possible or am I dreaming?
Sub FilterTableContent()
'A basic Word macro coded by Greg Maxey
Dim oDoc As Document
Dim oTbl As Table
Dim oCell As Cell
Dim strText As String, strRef As String
Dim lngCol As Long
Dim oRng As Range
ActiveDocument.Tables(1).Range.Copy
Set oDoc = Documents.Add
oDoc.Range.Paste
Set oTbl = oDoc.Tables(1)
strText = InputBox("Search text?")
lngCol = CLng(InputBox("Enter column to search", "Must be 1 - 4"))
For Each oCell In oTbl.Range.Cells
If oCell.RowIndex > 1 And oCell.ColumnIndex = lngCol Then
On Error Resume Next
'Assumes that reference cell is column 1
If Left(oTbl.Cell(oCell.RowIndex, 1).Range.Text, Len(oTbl.Cell(oCell.RowIndex, 1).Range.Text) - 2) <> vbNullString Then
strRef = Left(oTbl.Cell(oCell.RowIndex, 1).Range.Text, Len(oTbl.Cell(oCell.RowIndex, 1).Range.Text) - 2)
End If
If Left(oTbl.Cell(oCell.RowIndex, 1).Range.Text, Len(oTbl.Cell(oCell.RowIndex, 1).Range.Text) - 2) = vbNullString Then
oTbl.Cell(oCell.RowIndex, 1).Range.Text = strRef
End If
On Error GoTo 0
If Not InStr(UCase(oCell.Range.Text), UCase(strText)) > 0 Then
'If the base string isn't found then kill the row.
oCell.Range.Select
Selection.Rows.Delete
Else
'The base string is found so look for the specific string.
Set oRng = oCell.Range
'oRng.End = oRng.End - 1
With oRng.Find
.Text = strText
.MatchWholeWord = True
If Not .Execute Then
oCell.Range.Select
Selection.Rows.Delete
End If
End With
End If
End If
Next
lbl_Exit:
Set oDoc = Nothing: Set oTbl = Nothing: Set oCell = Nothing
Exit Sub
End Sub