I have following data in Excel
I need above data in below format. Kindly let me know VBA code for this.
As elzibethII has child Charles & charles has child then charles will be in the next line after elzibeth & same for the remaining.
I have try different VBA codes with different scenarios but unfortunately its not quite successful
The code is given below
Sub final()
Dim i, j, a, lrr, b, c, d, e, f As Long
Dim nm, nm1, nm2 As String
Dim rng As Range
lrr = Sheet5.Cells(Rows.Count, "A").End(xlUp).Row
i = 1
'finding max number
If lrr > 1 Then
Set rng = Sheet5.Range("A1:A" & lrr)
a = Application.WorksheetFunction.Max(rng)
End If
b = 2
c = 2
For i = 2 To lrr
nm = Sheet5.Cells(b, 13).Value
For c = i + 1 To lrr
If Sheet5.Cells(c, 12).Value = nm Then
Sheet5.Cells(c + 1, 16).Value = Sheet5.Cells(c, 11).Value
Sheet5.Cells(c + 1, 17).Value = Sheet5.Cells(c, 12).Value
Sheet5.Cells(c + 1, 18).Value = Sheet5.Cells(c, 13).Value
Sheet5.Cells(c + 1, 19).Value = Sheet5.Cells(c, 14).Value
'a = a + 1
c = c + 1
e = Sheet5.Cells(c, 16).Value
' nm2 = Sheet5.Cells(c, 18).Value
While e <= a
For j = c To lrr
nm2 = Sheet5.Cells(c, 18).Value
If Sheet5.Cells(j, 12).Value = nm2 Then
'e = Sheet5.Cells(c, 16).Value
Sheet5.Cells(c + 1, 16).Value = Sheet5.Cells(j, 11).Value
Sheet5.Cells(c + 1, 17).Value = Sheet5.Cells(j, 12).Value
Sheet5.Cells(c + 1, 18).Value = Sheet5.Cells(j, 13).Value
Sheet5.Cells(c + 1, 19).Value = Sheet5.Cells(j, 14).Value
'a = a + 1
c = c + 1
e = Sheet5.Cells(c, 16).Value
If e = 5 Then
nm2 = Sheet5.Cells(c, 17).Value
f = j
For f = j + 1 To lrr
If Sheet5.Cells(f, 12).Value = nm2 Then
Sheet5.Cells(c + 1, 16).Value = Sheet5.Cells(f, 11).Value
Sheet5.Cells(c + 1, 17).Value = Sheet5.Cells(f, 12).Value
Sheet5.Cells(c + 1, 18).Value = Sheet5.Cells(f, 13).Value
Sheet5.Cells(c + 1, 19).Value = Sheet5.Cells(f, 14).Value
'a = a + 1
c = c + 1
End If
Next
End If
End If
Next
e = e + 1
Wend
End If
Next
Next
End Sub
i am getting till 1st part and code is not going for the other parts as given below.



Microsoft documentation: