Hi
Another option
Assuming a contiguous list starting in A2:B2, result in F2:G2 down, paste in a module and execute:
Code:
Option Explicit
Dim rParents As Range
Dim lRow As Long
Sub ListDescendents()
Set rParents = Range("A2", Range("A2").End(xlDown))
lRow = 2
ListDescendents1 "", ""
Range("F2", Range("G2").End(xlDown)).Sort key1:=Range("F2"), order1:=xlAscending, key2:=Range("G2"), order2:=xlAscending
End Sub
Sub ListDescendents1(sChild As String, sAncestor As String)
Dim r As Range
For Each r In rParents
If r.Value = sChild Or sAncestor = "" Then
Range("F" & lRow) = IIf(sAncestor = "", r.Value, sAncestor)
Range("G" & lRow) = r.Offset(0, 1).Value
lRow = lRow + 1
ListDescendents1 r.Offset(0, 1).Value, IIf(sAncestor = "", r.Value, sAncestor)
End If
Next r
End Sub
Example:
[TABLE="width: 2"]
<tbody>[TR]
[TH] [/TH]
[TH="align: center"]A[/TH]
[TH="align: center"]B[/TH]
[TH="width: 30, align: center"]C[/TH]
[TH="width: 30, align: center"]D[/TH]
[TH="width: 30, align: center"]E[/TH]
[TH="align: center"]F[/TH]
[TH="align: center"]G[/TH]
[TH="width: 30, align: center"]H[/TH]
[/TR]
[TR]
[TD="align: center"]
1[/TD]
[TD="align: left"]Parent[/TD]
[TD="align: left"]Child[/TD]
[TD="align: right"] [/TD]
[TD="align: right"] [/TD]
[TD="align: right"] [/TD]
[TD="align: left"]Element[/TD]
[TD="align: left"]Descendents[/TD]
[TD="align: right"] [/TD]
[/TR]
[TR]
[TD="align: center"]
2[/TD]
[TD="align: left"]A[/TD]
[TD="align: left"]B[/TD]
[TD="align: right"] [/TD]
[TD="align: right"] [/TD]
[TD="align: right"] [/TD]
[TD="align: left"]A[/TD]
[TD="align: left"]B[/TD]
[TD="align: right"] [/TD]
[/TR]
[TR]
[TD="align: center"]
3[/TD]
[TD="align: left"]B[/TD]
[TD="align: left"]C[/TD]
[TD="align: right"] [/TD]
[TD="align: right"] [/TD]
[TD="align: right"] [/TD]
[TD="align: left"]A[/TD]
[TD="align: left"]C[/TD]
[TD="align: right"] [/TD]
[/TR]
[TR]
[TD="align: center"]
4[/TD]
[TD="align: left"]C[/TD]
[TD="align: left"]D[/TD]
[TD="align: right"] [/TD]
[TD="align: right"] [/TD]
[TD="align: right"] [/TD]
[TD="align: left"]A[/TD]
[TD="align: left"]D[/TD]
[TD="align: right"] [/TD]
[/TR]
[TR]
[TD="align: center"]
5[/TD]
[TD="align: left"]E[/TD]
[TD="align: left"]F[/TD]
[TD="align: right"] [/TD]
[TD="align: right"] [/TD]
[TD="align: right"] [/TD]
[TD="align: left"]A[/TD]
[TD="align: left"]E[/TD]
[TD="align: right"] [/TD]
[/TR]
[TR]
[TD="align: center"]
6[/TD]
[TD="align: left"]F[/TD]
[TD="align: left"]G[/TD]
[TD="align: right"] [/TD]
[TD="align: right"] [/TD]
[TD="align: right"] [/TD]
[TD="align: left"]A[/TD]
[TD="align: left"]F[/TD]
[TD="align: right"] [/TD]
[/TR]
[TR]
[TD="align: center"]
7[/TD]
[TD="align: left"]B[/TD]
[TD="align: left"]H[/TD]
[TD="align: right"] [/TD]
[TD="align: right"] [/TD]
[TD="align: right"] [/TD]
[TD="align: left"]A[/TD]
[TD="align: left"]G[/TD]
[TD="align: right"] [/TD]
[/TR]
[TR]
[TD="align: center"]
8[/TD]
[TD="align: left"]A[/TD]
[TD="align: left"]I[/TD]
[TD="align: right"] [/TD]
[TD="align: right"] [/TD]
[TD="align: right"] [/TD]
[TD="align: left"]A[/TD]
[TD="align: left"]H[/TD]
[TD="align: right"] [/TD]
[/TR]
[TR]
[TD="align: center"]
9[/TD]
[TD="align: left"]D[/TD]
[TD="align: left"]K[/TD]
[TD="align: right"] [/TD]
[TD="align: right"] [/TD]
[TD="align: right"] [/TD]
[TD="align: left"]A[/TD]
[TD="align: left"]I[/TD]
[TD="align: right"] [/TD]
[/TR]
[TR]
[TD="align: center"]
10[/TD]
[TD="align: left"]K[/TD]
[TD="align: left"]J[/TD]
[TD="align: right"] [/TD]
[TD="align: right"] [/TD]
[TD="align: right"] [/TD]
[TD="align: left"]A[/TD]
[TD="align: left"]J[/TD]
[TD="align: right"] [/TD]
[/TR]
[TR]
[TD="align: center"]
11[/TD]
[TD="align: left"]C[/TD]
[TD="align: left"]E[/TD]
[TD="align: right"] [/TD]
[TD="align: right"] [/TD]
[TD="align: right"] [/TD]
[TD="align: left"]A[/TD]
[TD="align: left"]K[/TD]
[TD="align: right"] [/TD]
[/TR]
[TR]
[TD="align: center"]
12[/TD]
[TD="align: left"]K[/TD]
[TD="align: left"]L[/TD]
[TD="align: right"] [/TD]
[TD="align: right"] [/TD]
[TD="align: right"] [/TD]
[TD="align: left"]A[/TD]
[TD="align: left"]L[/TD]
[TD="align: right"] [/TD]
[/TR]
[TR]
[TD="align: center"]
13[/TD]
[TD="align: left"]J[/TD]
[TD="align: left"]M[/TD]
[TD="align: right"] [/TD]
[TD="align: right"] [/TD]
[TD="align: right"] [/TD]
[TD="align: left"]A[/TD]
[TD="align: left"]M[/TD]
[TD="align: right"] [/TD]
[/TR]
[TR]
[TD="align: center"]
14[/TD]
[TD="align: right"] [/TD]
[TD="align: right"] [/TD]
[TD="align: right"] [/TD]
[TD="align: right"] [/TD]
[TD="align: right"] [/TD]
[TD="align: left"]B[/TD]
[TD="align: left"]C[/TD]
[TD="align: right"] [/TD]
[/TR]
[TR]
[TD="align: center"]
15[/TD]
[TD="align: right"] [/TD]
[TD="align: right"] [/TD]
[TD="align: right"] [/TD]
[TD="align: right"] [/TD]
[TD="align: right"] [/TD]
[TD="align: left"]B[/TD]
[TD="align: left"]D[/TD]
[TD="align: right"] [/TD]
[/TR]
[TR]
[TD="align: center"]
16[/TD]
[TD="align: right"] [/TD]
[TD="align: right"] [/TD]
[TD="align: right"] [/TD]
[TD="align: right"] [/TD]
[TD="align: right"] [/TD]
[TD="align: left"]B[/TD]
[TD="align: left"]E[/TD]
[TD="align: right"] [/TD]
[/TR]
[TR]
[TD="align: center"]
17[/TD]
[TD="align: right"] [/TD]
[TD="align: right"] [/TD]
[TD="align: right"] [/TD]
[TD="align: right"] [/TD]
[TD="align: right"] [/TD]
[TD="align: left"]B[/TD]
[TD="align: left"]F[/TD]
[TD="align: right"] [/TD]
[/TR]
[TR]
[TD="align: center"]
18[/TD]
[TD="align: right"] [/TD]
[TD="align: right"] [/TD]
[TD="align: right"] [/TD]
[TD="align: right"] [/TD]
[TD="align: right"] [/TD]
[TD="align: left"]B[/TD]
[TD="align: left"]G[/TD]
[TD="align: right"] [/TD]
[/TR]
[TR]
[TD="align: center"]
19[/TD]
[TD="align: right"] [/TD]
[TD="align: right"] [/TD]
[TD="align: right"] [/TD]
[TD="align: right"] [/TD]
[TD="align: right"] [/TD]
[TD="align: left"]B[/TD]
[TD="align: left"]H[/TD]
[TD="align: right"] [/TD]
[/TR]
[TR]
[TD="align: center"]
20[/TD]
[TD="align: right"] [/TD]
[TD="align: right"] [/TD]
[TD="align: right"] [/TD]
[TD="align: right"] [/TD]
[TD="align: right"] [/TD]
[TD="align: left"]B[/TD]
[TD="align: left"]J[/TD]
[TD="align: right"] [/TD]
[/TR]
[TR]
[TD="align: center"]
21[/TD]
[TD="align: right"] [/TD]
[TD="align: right"] [/TD]
[TD="align: right"] [/TD]
[TD="align: right"] [/TD]
[TD="align: right"] [/TD]
[TD="align: left"]B[/TD]
[TD="align: left"]K[/TD]
[TD="align: right"] [/TD]
[/TR]
[TR]
[TD="align: center"]
22[/TD]
[TD="align: right"] [/TD]
[TD="align: right"] [/TD]
[TD="align: right"] [/TD]
[TD="align: right"] [/TD]
[TD="align: right"] [/TD]
[TD="align: left"]B[/TD]
[TD="align: left"]L[/TD]
[TD="align: right"] [/TD]
[/TR]
[TR]
[TD="align: center"]
23[/TD]
[TD="align: right"] [/TD]
[TD="align: right"] [/TD]
[TD="align: right"] [/TD]
[TD="align: right"] [/TD]
[TD="align: right"] [/TD]
[TD="align: left"]B[/TD]
[TD="align: left"]M[/TD]
[TD="align: right"] [/TD]
[/TR]
[TR]
[TD="align: center"]
24[/TD]
[TD="align: right"] [/TD]
[TD="align: right"] [/TD]
[TD="align: right"] [/TD]
[TD="align: right"] [/TD]
[TD="align: right"] [/TD]
[TD="align: left"]C[/TD]
[TD="align: left"]D[/TD]
[TD="align: right"] [/TD]
[/TR]
[TR]
[TD="align: center"]
25[/TD]
[TD="align: right"] [/TD]
[TD="align: right"] [/TD]
[TD="align: right"] [/TD]
[TD="align: right"] [/TD]
[TD="align: right"] [/TD]
[TD="align: left"]C[/TD]
[TD="align: left"]E[/TD]
[TD="align: right"] [/TD]
[/TR]
[TR]
[TD="align: center"]
26[/TD]
[TD="align: right"] [/TD]
[TD="align: right"] [/TD]
[TD="align: right"] [/TD]
[TD="align: right"] [/TD]
[TD="align: right"] [/TD]
[TD="align: left"]C[/TD]
[TD="align: left"]F[/TD]
[TD="align: right"] [/TD]
[/TR]
[TR]
[TD="align: center"]
27[/TD]
[TD="align: right"] [/TD]
[TD="align: right"] [/TD]
[TD="align: right"] [/TD]
[TD="align: right"] [/TD]
[TD="align: right"] [/TD]
[TD="align: left"]C[/TD]
[TD="align: left"]G[/TD]
[TD="align: right"] [/TD]
[/TR]
[TR]
[TD="align: center"]
28[/TD]
[TD="align: right"] [/TD]
[TD="align: right"] [/TD]
[TD="align: right"] [/TD]
[TD="align: right"] [/TD]
[TD="align: right"] [/TD]
[TD="align: left"]C[/TD]
[TD="align: left"]J[/TD]
[TD="align: right"] [/TD]
[/TR]
[TR]
[TD="align: center"]
29[/TD]
[TD="align: right"] [/TD]
[TD="align: right"] [/TD]
[TD="align: right"] [/TD]
[TD="align: right"] [/TD]
[TD="align: right"] [/TD]
[TD="align: left"]C[/TD]
[TD="align: left"]K[/TD]
[TD="align: right"] [/TD]
[/TR]
[TR]
[TD="align: center"]
30[/TD]
[TD="align: right"] [/TD]
[TD="align: right"] [/TD]
[TD="align: right"] [/TD]
[TD="align: right"] [/TD]
[TD="align: right"] [/TD]
[TD="align: left"]C[/TD]
[TD="align: left"]L[/TD]
[TD="align: right"] [/TD]
[/TR]
[TR]
[TD="align: center"]
31[/TD]
[TD="align: right"] [/TD]
[TD="align: right"] [/TD]
[TD="align: right"] [/TD]
[TD="align: right"] [/TD]
[TD="align: right"] [/TD]
[TD="align: left"]C[/TD]
[TD="align: left"]M[/TD]
[TD="align: right"] [/TD]
[/TR]
[TR]
[TD="align: center"]
32[/TD]
[TD="align: right"] [/TD]
[TD="align: right"] [/TD]
[TD="align: right"] [/TD]
[TD="align: right"] [/TD]
[TD="align: right"] [/TD]
[TD="align: left"]D[/TD]
[TD="align: left"]J[/TD]
[TD="align: right"] [/TD]
[/TR]
[TR]
[TD="align: center"]
33[/TD]
[TD="align: right"] [/TD]
[TD="align: right"] [/TD]
[TD="align: right"] [/TD]
[TD="align: right"] [/TD]
[TD="align: right"] [/TD]
[TD="align: left"]D[/TD]
[TD="align: left"]K[/TD]
[TD="align: right"] [/TD]
[/TR]
[TR]
[TD="align: center"]
34[/TD]
[TD="align: right"] [/TD]
[TD="align: right"] [/TD]
[TD="align: right"] [/TD]
[TD="align: right"] [/TD]
[TD="align: right"] [/TD]
[TD="align: left"]D[/TD]
[TD="align: left"]L[/TD]
[TD="align: right"] [/TD]
[/TR]
[TR]
[TD="align: center"]
35[/TD]
[TD="align: right"] [/TD]
[TD="align: right"] [/TD]
[TD="align: right"] [/TD]
[TD="align: right"] [/TD]
[TD="align: right"] [/TD]
[TD="align: left"]D[/TD]
[TD="align: left"]M[/TD]
[TD="align: right"] [/TD]
[/TR]
[TR]
[TD="align: center"]
36[/TD]
[TD="align: right"] [/TD]
[TD="align: right"] [/TD]
[TD="align: right"] [/TD]
[TD="align: right"] [/TD]
[TD="align: right"] [/TD]
[TD="align: left"]E[/TD]
[TD="align: left"]F[/TD]
[TD="align: right"] [/TD]
[/TR]
[TR]
[TD="align: center"]
37[/TD]
[TD="align: right"] [/TD]
[TD="align: right"] [/TD]
[TD="align: right"] [/TD]
[TD="align: right"] [/TD]
[TD="align: right"] [/TD]
[TD="align: left"]E[/TD]
[TD="align: left"]G[/TD]
[TD="align: right"] [/TD]
[/TR]
[TR]
[TD="align: center"]
38[/TD]
[TD="align: right"] [/TD]
[TD="align: right"] [/TD]
[TD="align: right"] [/TD]
[TD="align: right"] [/TD]
[TD="align: right"] [/TD]
[TD="align: left"]F[/TD]
[TD="align: left"]G[/TD]
[TD="align: right"] [/TD]
[/TR]
[TR]
[TD="align: center"]
39[/TD]
[TD="align: right"] [/TD]
[TD="align: right"] [/TD]
[TD="align: right"] [/TD]
[TD="align: right"] [/TD]
[TD="align: right"] [/TD]
[TD="align: left"]J[/TD]
[TD="align: left"]M[/TD]
[TD="align: right"] [/TD]
[/TR]
[TR]
[TD="align: center"]
40[/TD]
[TD="align: right"] [/TD]
[TD="align: right"] [/TD]
[TD="align: right"] [/TD]
[TD="align: right"] [/TD]
[TD="align: right"] [/TD]
[TD="align: left"]K[/TD]
[TD="align: left"]J[/TD]
[TD="align: right"] [/TD]
[/TR]
[TR]
[TD="align: center"]
41[/TD]
[TD="align: right"] [/TD]
[TD="align: right"] [/TD]
[TD="align: right"] [/TD]
[TD="align: right"] [/TD]
[TD="align: right"] [/TD]
[TD="align: left"]K[/TD]
[TD="align: left"]L[/TD]
[TD="align: right"] [/TD]
[/TR]
[TR]
[TD="align: center"]
42[/TD]
[TD="align: right"] [/TD]
[TD="align: right"] [/TD]
[TD="align: right"] [/TD]
[TD="align: right"] [/TD]
[TD="align: right"] [/TD]
[TD="align: left"]K[/TD]
[TD="align: left"]M[/TD]
[TD="align: right"] [/TD]
[/TR]
[TR]
[TD="align: center"]
43[/TD]
[TD="align: right"] [/TD]
[TD="align: right"] [/TD]
[TD="align: right"] [/TD]
[TD="align: right"] [/TD]
[TD="align: right"] [/TD]
[TD="align: right"] [/TD]
[TD="align: right"] [/TD]
[TD="align: right"] [/TD]
[/TR]
[TR]
[TD="colspan: 9"] [Book1]Sheet1[/TD]
[/TR]
</tbody>[/TABLE]