Create parent child hierarchy from flat file

lemeister

New Member
Joined
Nov 20, 2003
Messages
48
I am looking to write some code that will take a flat file and create a child/parent hierarchy from it. The flat file is sorted in order so that each new row is either a new level in the heirarchy or a new child member.

This is a sample of the sorted flat file from which the hierarch should be created:

Book1
ABCDE
1DimensionIn/OutTypeP#L#
2ContractInPolProg1Prog1L1
3ContractInPolProg1Prog1L2
4ContractInPolProg1Prog1L3
5ContractInPolProg2Prog2L1
6ContractInPolProg2Prog2L2
7ContractInPolProg2Prog2L3
8ContractOutNonPolProg3Prog3L1
Sheet2


This is the expected format of the hierarchy:


Book1
ABCDEFGH
11ContractChildParent
12+InInContract
13+PolPolIn
14+Prog1Prog1Pol
15+Prog1L1Prog1L1Prog1
16+Prog1L2Prog1L2Prog1
17+Prog1L3Prog1L3Prog1
18+Prog2Prog2Pol
19+Prog2L1Prog2L1Prog2
20+Prog2L2Prog2L2Prog2
21+Prog2L3Prog2L3Prog2
22+OutOutContract
23+NonPolNonPolOut
24+Prog3Prog3NonPol
25+Prog3L1Prog3L1Prog3
Sheet2


I am reasonably proficient in VBA but I am struggling with the correct logic to construct this hierarchy, as in should I be starting at the top or bottom of the flat file and how to create the cascading hierarchy. Any help appreciated.
 

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
Try this for results on sheet2.
Rich (BB code):
Sub MG28Feb19
Dim Rng As Range, Dn As Range, n As Long, Q As Variant, col As Long
Dim K As Variant, Ac As Long, c As Long
Dim R As Range, Temp As String, St As Long, Dic As Object
Set Rng = Range("D2", Range("D" & Rows.Count).End(xlUp))
Set Dic = CreateObject("scripting.dictionary")
Dic.CompareMode = vbTextCompare
For Each Dn In Rng
    If Not Dic.Exists(Dn.Value) Then
        Dic.Add Dn.Value, Array(Dn, Dn.Offset(, -3))
    Else
         Q = Dic(Dn.Value)
         Set Q(0) = Union(Q(0), Dn)
         Dic(Dn.Value) = Q
    End If
Next
With Sheets("Sheet2")
    .Cells(1, 1) = ""
    c = 1
For Each K In Dic.keys
        If Temp = "" Then
            .Cells(1, 1) = Dic(K)(1)
            .Cells(2, 1) = "+"
            .Cells(1, 7) = "Child"
            .Cells(1, 8) = "Parent"
            St = 1: Temp = Dic(K)(1).Offset(, 1)
        ElseIf Not Dic(K)(1).Offset(, 1) = Temp Then
            St = 1: Temp = Dic(K)(1).Offset(, 1)
        Else
            St = 3
        End If
    For Ac = St To 4
       If Ac = 4 Then
            For Each R In Dic(K)(0).Offset(, 1)
                c = c + 1
                .Cells(c, Ac + 1) = R.Value
                .Cells(c, Ac) = "+"
                .Cells(c, 7) = R.Value
                .Cells(c, 8) = Dic(K)(1).Offset(, Ac - 1)
            Next R
        Else
             c = c + 1
             If Ac = 3 Then .Cells(c, Ac) = "+"
             .Cells(c, Ac + 1) = Dic(K)(1).Offset(, Ac)
             .Cells(c + 1, Ac + 1) = "+"
             .Cells(c, 7) = Dic(K)(1).Offset(, Ac)
             .Cells(c, 8) = Dic(K)(1).Offset(, Ac - 1)
        End If
    Next Ac
Next K
    With .Range("A1").Resize(c, 8)
        .Columns.AutoFit
        .Borders.Weight = 2
    End With

End With
End Sub
Regards Mick
 
Upvote 1
Hi Mick,

As a novice I would like to ask how to put this code into power query.
Could you please help? Thanks!
 
Upvote 0

Forum statistics

Threads
1,215,480
Messages
6,125,053
Members
449,206
Latest member
Healthydogs

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