The merged rows are a pain to deal with, but thankfully you don't have merged columns as well. The macro I posted earlier requires a few minor changes to the code to enable it to be used with variable texts and column numbers. That's the easy part.
The more complicated part is to grab all the rows, fill in the missing data, and then work with the new table. The following may not be the simplest version of the code, but it does work and I have spent enough time on it. If you want to tidy it up further - feel free
Option Explicit
Sub ExtractRowData()
'Graham Mayor - http://www.gmayor.com - 30/10/2016
Dim oCell As Cell
Dim oRng As Range, oNum As Range
Dim oNewRng As Range
Dim oNewCell As Range
Dim oTable As Table
Dim oNewTable As Table
Dim oDoc As Document
Dim oTemp As Document
Dim iCol As Long, iRow As Long
Set oDoc = ActiveDocument
Set oTemp = Documents.Add
Set oTable = oDoc.Tables(1)
Set oNewTable = oTemp.Tables.Add(oTemp.Range, oTable.Rows.Count, oTable.Columns.Count)
For Each oCell In oTable.Range.Cells
iRow = oCell.RowIndex
iCol = oCell.ColumnIndex
Set oRng = oCell.Range
oRng.End = oRng.End - 1
oNewTable.Cell(iRow, iCol).Range.FormattedText = oRng.FormattedText
Next oCell
With oNewTable
For iRow = 2 To .Rows.Count
Set oNewRng = .Cell(iRow, 1).Range
oNewRng.End = oNewRng.End - 1
If oNewRng.Text = "" Then
Set oNum = .Cell(iRow - 1, 1).Range
oNum.End = oNum.End - 1
oNewRng = oNum
End If
Next iRow
End With
CopyRows oTemp
lbl_Exit:
Set oDoc = Nothing
Set oTemp = Nothing
Set oTable = Nothing
Set oNewTable = Nothing
Set oCell = Nothing
Set oRng = Nothing
Set oNewRng = Nothing
Set oNum = Nothing
Exit Sub
End Sub
Sub CopyRows(oSource As Document)
'Graham Mayor - http://www.gmayor.com - 28/10/2016
'Updated 30/10/2016
Dim oSourceTable As Table
Dim oTargetTable As Table
Dim oTarget As Document
Dim oCell As Range
Dim oRng As Range
Dim oRow As Row
Dim i As Long, j As Long
Dim strWord As String, strCol As String
'identify the source table
Set oSourceTable = oSource.Tables(1)
strWord = InputBox("Enter word or phrase to find")
If strWord = "" Then GoTo lbl_Exit
Again:
strCol = InputBox("Find in which column?")
If Not IsNumeric(strCol) Or _
val(strCol) > oSourceTable.Columns.Count _
Or val(strCol) = 0 Then
MsgBox "Enter a numeric value no greater than the number of columns in the table!"
GoTo Again:
End If
j = CLng(strCol)
'Create a new document to take the found rows
Set oTarget = Documents.Add
'Add a one row empty table with as many columns as the source table
'Note this will not work with merged or split cells/rows
Set oTargetTable = oTarget.Range.Tables.Add(oTarget.Range, 1, oSourceTable.Columns.Count)
'Check each rfow
For Each oRow In oSourceTable.Rows
'See if the target word is in cell 2
Set oCell = oRow.Cells(j).Range
If InStr(1, oCell.Text, strWord) > 0 Then
'if it is make sure we have a new empty row available
If InStr(1, oTargetTable.Rows.Last.Cells(j).Range.Text, strWord) > 0 Then
oTargetTable.Rows.Add
End If
'get the cell contents and reproduce them in the target table
For i = 1 To oRow.Cells.Count
Set oCell = oTargetTable.Rows.Last.Cells(i).Range
oCell.End = oCell.End - 1
Set oRng = oRow.Cells(i).Range
oRng.End = oRng.End - 1
oCell.FormattedText = oRng.FormattedText
Next i
End If
Next oRow
oSource.Close 0
lbl_Exit:
'clean up
Set oSource = Nothing
Set oTarget = Nothing
Set oSourceTable = Nothing
Set oTargetTable = Nothing
Set oRow = Nothing
Set oCell = Nothing
Set oRng = Nothing
Exit Sub
End Sub