Sub ReportInWord()
Dim ws As Worksheet
Dim lastRow As Long
Dim uniqueValues As New Collection
Dim cell As Range
Dim value As Variant
Dim wdApp As Object
Dim wdDoc As Object
Dim tblInfo As Object
Dim tblSteps As Object
Set ws = ThisWorkbook.Sheets("E2E")
lastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row
For Each cell In ws.Range("A2:A" & lastRow)
On Error Resume Next
uniqueValues.Add cell.value, CStr(cell.value)
On Error GoTo 0
Next cell
cellLoopTracker = 0
For Each value In uniqueValues
Set wdApp = CreateObject("Word.Application")
wdApp.Visible = True
Set wdDoc = wdApp.Documents.Add
With wdDoc
Dim rngInfo As Object
Set rngInfo = .Range
Set tblInfo = .Tables.Add(rngInfo, 7, 2)
tblInfo.Borders.Enable = True
tblInfo.cell(1, 1).Range.Text = ws.Range("A1").value
tblInfo.cell(1, 2).Range.Text = value
tblInfo.cell(2, 1).Range.Text = ws.Range("B1").value
tblInfo.cell(3, 1).Range.Text = ws.Range("C1").value
tblInfo.cell(4, 1).Range.Text = ws.Range("D1").value
tblInfo.cell(5, 1).Range.Text = ws.Range("I1").value
tblInfo.cell(6, 1).Range.Text = ws.Range("N1").value
tblInfo.cell(7, 1).Range.Text = ws.Range("O1").value
For cellLoop = 2 To lastRow
If ws.Range("A" & cellLoop).value = value Then
tblInfo.cell(2, 2).Range.Text = ws.Range("B" & cellLoop).value
tblInfo.cell(3, 2).Range.Text = ws.Range("C" & cellLoop).value
tblInfo.cell(4, 2).Range.Text = ws.Range("D" & cellLoop).value
tblInfo.cell(5, 2).Range.Text = ws.Range("I" & cellLoop).value
tblInfo.cell(6, 2).Range.Text = ws.Range("N" & cellLoop).value
tblInfo.cell(7, 2).Range.Text = ws.Range("O" & cellLoop).value
Exit For
End If
Next cellLoop
.Content.InsertParagraphAfter
End With
With wdDoc
Dim rngSteps As Object
Set rngSteps = .Range
rngSteps.Collapse 0
rngSteps.InsertParagraphAfter
Set tblSteps = .Tables.Add(rngSteps, 7, 3)
tblSteps.Borders.Enable = True
tblSteps.cell(1, 1).Range.Text = "Step No."
tblSteps.cell(1, 2).Range.Text = "Test Step Description"
tblSteps.cell(1, 3).Range.Text = "Expected Result"
previousCellLoop = 1
For cellLoop = 2 To lastRow
If ws.Range("A" & cellLoop).value = value Then
tblSteps.cell(cellLoop - previousCellLoop + 1, 1).Range.Text = ws.Range("J" & cellLoop).value
tblSteps.cell(cellLoop - previousCellLoop + 1, 2).Range.Text = ws.Range("K" & cellLoop).value
tblSteps.cell(cellLoop - previousCellLoop + 1, 3).Range.Text = ws.Range("L" & cellLoop).value
Else
previousCellLoop = cellLoop
End If
Next cellLoop
End With
Set tblInfo = Nothing
Set tblSteps = Nothing
Set wdDoc = Nothing
Set wdApp = Nothing
Next value
End Sub