Parent - Child Hierarchy

Kingsley MG

New Member
Joined
Aug 12, 2018
Messages
1
Data as below
[TABLE="width: 128"]
<tbody>[TR]
[TD="class: xl63"]Parent[/TD]
[TD="class: xl63, width: 64"]Child[/TD]
[/TR]
[TR]
[TD="class: xl63"]AAA[/TD]
[TD="class: xl63"]BBB[/TD]
[/TR]
[TR]
[TD="class: xl63"]BBB[/TD]
[TD="class: xl63"]CCC[/TD]
[/TR]
[TR]
[TD="class: xl63"]AAA[/TD]
[TD="class: xl63"]DDD[/TD]
[/TR]
[TR]
[TD="class: xl63"]DDD[/TD]
[TD="class: xl63"]EEE[/TD]
[/TR]
[TR]
[TD="class: xl63"]EEE[/TD]
[TD="class: xl63"]FFF[/TD]
[/TR]
[TR]
[TD="class: xl63"]CCC[/TD]
[TD="class: xl63"]GGG[/TD]
[/TR]
[TR]
[TD="class: xl63"]GGG[/TD]
[TD="class: xl63"]HHH[/TD]
[/TR]
[TR]
[TD="class: xl63"]HHH[/TD]
[TD="class: xl63"]III[/TD]
[/TR]
[TR]
[TD="class: xl63"]III[/TD]
[TD="class: xl63"]JJJ[/TD]
[/TR]
[TR]
[TD="class: xl63"]III[/TD]
[TD="class: xl63"]KKK[/TD]
[/TR]
[TR]
[TD="class: xl63"]JJJ[/TD]
[TD="class: xl63"]LLL[/TD]
[/TR]
[TR]
[TD="class: xl63"]KKK[/TD]
[TD="class: xl63"]MMM[/TD]
[/TR]
[TR]
[TD="class: xl63"]MMM[/TD]
[TD="class: xl63"]NNN[/TD]
[/TR]
</tbody>[/TABLE]
and continues.

Output should be collapsed. EX. as below
[TABLE="width: 297"]
<tbody>[TR]
[TD]AAA[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]+[/TD]
[TD]BBB[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/T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/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD]+[/TD]
[TD]FFF[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
</tbody>[/TABLE]
and continue as per the Parent - Child data.
 

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
hello. suggest you test it on further data, & modify as required if you find errors/deficiencies. cheers
Code:
Sub maybe()    
    'https://www.mrexcel.com/forum/excel-questions/1066730-parent-child-hierarchy.html
    
    'CODING ASSUMES input data is on worksheet called "input data" & results go to worksheet "output"
    
    Dim i As Long
    Dim lColumn As Long
    Dim sThisChild As String
    Dim sThisParent As String
    Dim rng As Excel.Range
    Dim ar As Variant
    
    ar = Worksheets("input data").Range("A1").CurrentRegion.Value2


    With Worksheets("output")
        .Cells.Clear
        'start with first data and populate worksheet..
        'Put first data in worksheet, from row 2 of input data
        .Cells(1, 1).Value2 = ar(2, 1)
        .Cells(2, 1).Value2 = "+"
        .Cells(2, 2).Value2 = ar(2, 2)
        
        'Now loop through all others
        For i = LBound(ar, 1) + 2 To UBound(ar, 1)
            sThisParent = ar(i, 1)
            sThisChild = ar(i, 2)
            
            'check if the parent is already in the worksheet
            Set rng = .Cells.Find(What:=sThisParent, LookAt:=xlWhole)
            
            If rng Is Nothing Then
                'Parent data is not yet on the worksheet.
                'This new data can be posted to the worksheet's first row or become the new last row.
                'Make it the last row for consistency with image posted in question.
                'Work out the current last row,
                Set rng = .Cells.Find(What:="*", After:=.Range("A1"), SearchOrder:=xlByRows, SearchDirection:=xlPrevious)
                .Cells(rng.Row + 1, 1) = sThisParent
                .Cells(rng.Row + 2, 1).Value2 = "+"
                .Cells(rng.Row + 2, 2) = sThisChild
            Else
                'Parent data is already on the worksheet. So add this data as a child to it.
                'if there is not yet a child,
                If Len(rng.Offset(1, 1).Value2) = 0 Then
                    rng.Offset(1).EntireRow.Insert
                    rng.Offset(1).Value2 = "+"
                    rng.Offset(1, 1).Value2 = sThisChild
                Else
                    'find end of this branch
                    lColumn = rng.Column
                    Do
                        Set rng = rng.Offset(1, 1)
                    Loop Until Len(rng.Offset(1, 1).Value2) = 0
                    rng.Offset(1).EntireRow.Insert
                    .Cells(rng.Row + 1, lColumn).Value2 = "+"
                    .Cells(rng.Row + 1, lColumn + 1).Value2 = sThisChild
                End If
            End If
            
        Next i
    End With


End Sub
 
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,289
Members
452,631
Latest member
a_potato

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top