Parent Child hierarchical format using Level

tandv

New Member
Joined
Dec 25, 2014
Messages
7
Hi,
I am trying to establish below in hierarchical format. Is there anyway if we can achieve output as below. tried grouping but didn't work.
Input
Node Level
A100 1
101Child 12
102Child 23
103Child 34
104Child 45
A200 1
201Child 12
202Child 23
203Child 34

<colgroup><col width="64" style="width:48pt" span="3"> </colgroup><tbody>
</tbody>


And output will be
A100
101Child 1
102Child 2
103Child 3
104Child 4
A200
201Child 1
202Child 2
203Child 3

<colgroup><col width="64" style="width:48pt" span="6"> </colgroup><tbody>
</tbody>
 

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
Try this for results in sheet 2.
Code:
[COLOR=navy]Sub[/COLOR] MG10Oct38
[COLOR=navy]Dim[/COLOR] Rng [COLOR=navy]As[/COLOR] Range, Dn [COLOR=navy]As[/COLOR] Range, c [COLOR=navy]As[/COLOR] [COLOR=navy]Long,[/COLOR] Ac [COLOR=navy]As[/COLOR] [COLOR=navy]Long,[/COLOR] R [COLOR=navy]As[/COLOR] Range, n [COLOR=navy]As[/COLOR] [COLOR=navy]Long[/COLOR]
[COLOR=navy]Set[/COLOR] Rng = Range("B:B").SpecialCells(xlCellTypeConstants)
[COLOR=navy]For[/COLOR] [COLOR=navy]Each[/COLOR] Dn [COLOR=navy]In[/COLOR] Rng.Areas
[COLOR=navy]Set[/COLOR] Dn = Dn.Offset(-1, -1).Resize(Dn.Count + 1)
    Ac = 0
        [COLOR=navy]For[/COLOR] [COLOR=navy]Each[/COLOR] R [COLOR=navy]In[/COLOR] Dn
            c = c + 1: Ac = Ac + 1
              [COLOR=navy]With[/COLOR] Sheets("Sheet2")
                [COLOR=navy]If[/COLOR] c = 1 [COLOR=navy]Then[/COLOR]
                    .Cells(c, Ac) = R.Value
                [COLOR=navy]Else[/COLOR]
                    .Cells(c, Ac).Resize(, 2).Value = R.Resize(, 2).Value
                [COLOR=navy]End[/COLOR] If
             [COLOR=navy]End[/COLOR] With
        [COLOR=navy]Next[/COLOR] R
[COLOR=navy]Next[/COLOR] Dn
[COLOR=navy]End[/COLOR] [COLOR=navy]Sub[/COLOR]
Regards Mick
 
Last edited:
Upvote 0
Thanks Mick.
I tried to run code on Sheet 2 but it gives me run time error '1004' . Seems it can't find SpecialCells(xlCellTypeConstants)
 
Upvote 0
The sheet with your data needs to be the active sheet when you run the code
 
Upvote 0
Thanks. It works correctly when there are one child at each level however if there are multiple children at same level then it didn;t give correct. please refer below
Node Level
A100 1
101Child 12
102Child 23
103Child 34
104Child 45
105Child 55
106Child 65
A200 1
201Child 12
202Child 23
203Child 34

<colgroup><col width="64" style="width:48pt" span="3"> </colgroup><tbody>
</tbody>

Expected output
A100
101Child 1
102Child 2
103Child 3
104Child 4
105Child 5
106Child 6
A200
201Child 1
202Child 2
203Child 3

<colgroup><col width="64" style="width:48pt" span="6"> </colgroup><tbody>
</tbody>

however it gives as below

A100
101Child 1
102Child 2
103Child 3
104Child 4
105Child 5
106Child 6
A200
201Child 1
202Child 2
203Child 3


<colgroup><col width="64" style="width:48pt" span="8"> </colgroup><tbody>
</tbody>
 
Upvote 0
.. when there are one child at each level however if there are multiple children at same level then it didn;t give correct.
Try this then
Code:
Sub Hierarchy()  Dim c As Range
  
  Application.ScreenUpdating = False
  For Each c In Range("A2", Range("A" & Rows.Count).End(xlUp))
      c.Offset(, 3 + c.Offset(, 2).Value).Resize(, 2).Value = c.Resize(, 2).Value
  Next c
  Application.ScreenUpdating = True
End Sub

My sample data in A:C, results of code in E:J


Excel 2016
ABCDEFGHIJ
1NodeLevel
2A1001A100
3101Child 12101Child 1
4102Child 23102Child 2
5103Child 34103Child 3
6104Child 45104Child 4
7105Child 55105Child 5
8106Child 65106Child 6
9A2001A200
10201Child 12201Child 1
11202Child 23202Child 2
12203Child 34203Child 3
Sheet1
 
Upvote 0
Note that this result can also be achieved by formulas. Formula shown in E2 is copied across and down.

Excel Workbook
ABCDEFGHIJ
1NodeLevel
2A1001A100
3101Child 12101Child 1
4102Child 23102Child 2
5103Child 34103Child 3
6104Child 45104Child 4
7105Child 55105Child 5
8106Child 65106Child 6
9A2001A200
10201Child 12201Child 1
11202Child 23202Child 2
12203Child 34203Child 3
Sheet3
 
Upvote 0

Forum statistics

Threads
1,215,054
Messages
6,122,895
Members
449,097
Latest member
dbomb1414

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