Derive Heirearchy Level from Range

prab76

New Member
Joined
Apr 27, 2010
Messages
4
I have a range where I need to derive the level/depth in the hierarchy for each cell where some cells are comma seperated , for example:

TaskSubTaskParentTask
T1T2T0
T5T3,T4
T8T2

<tbody>
</tbody>


In this example range, the level of T0, T5 is 1. The level of T1,T3,T4 is 2. The level of T2 is 3, The level of T8 is 4, I need an output like this

TaskLevel
T01
T51
T12
T32
T42
T23
T84

<tbody>
</tbody>


Can anyone please provide direction in Excel how to achieve this
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
Welcome to the Board.

This requires a VBA macro. To try it, open a copy of your workbook, open to the page with data. Right click on the sheet tab on the bottom and select View Code. From the VBA editor menu, click Insert > Module. In the window that opens, paste this code:

Code:
Sub GetLevels()
Dim i As Long, lvl As Long, Tasks As Object, Lvls As Object, MyData As Variant, x As Variant, w As Variant
    
    Set Tasks = CreateObject("Scripting.Dictionary")
    Set Lvls = CreateObject("Scripting.dictionary")
    
    MyData = Range("A2:C" & Cells(Rows.Count, "A").End(xlUp).Row).Value
    For i = 1 To UBound(MyData)
        Tasks(MyData(i, 1)) = MyData(i, 3)
        If Not Tasks.exists(MyData(i, 3)) And MyData(i, 3) <> "" Then Tasks(MyData(i, 3)) = ""
        For Each x In Split(MyData(i, 2), ",")
            Tasks(x) = MyData(i, 1)
        Next x
    Next i
    
    For Each x In Tasks
        lvl = 1
        w = x
        Do While Tasks.exists(w)
            w = Tasks(w)
            If w = "" Then Exit Do
            lvl = lvl + 1
        Loop
        Lvls(x) = lvl
    Next x
    
    Range("E1:F1") = Array("Task", "Level")
    Range("E2").Resize(Tasks.Count).Value = WorksheetFunction.Transpose(Tasks.keys)
    Range("F2").Resize(Lvls.Count).Value = WorksheetFunction.Transpose(Lvls.items)
    
    With ActiveSheet.Sort
        .SortFields.Clear
        .SortFields.Add Key:=Range("F2")
        .SetRange Range("E:F")
        .Header = xlYes
        .Apply
    End With
    
End Sub
Press Alt-Q to close the editor. Press Alt-F8 to open the macro selector. Choose GetLevels and click Run.

This looks down column A as far as there is data. It assumes the only comma delimited cells are in column B. It will place the response table in columns E:F. Hope this helps.
 
Upvote 0
I've tried to develop a formula based solution rather than use VBA like Eric, and have got the correct results for all tasks EXCEPT sub-tasks T3 & T4 which are both shown in one cell of the data table delimited by a comma! That one outlier entry is giving me a headache :oops:, as I can't find a formula that will return the cell reference or cell position within a range of cells of the cell containing the ID of a task (i.e. Tn)

As an aside, I have one question:
How does a task NOT have a parent task? (i.e A Sub-Task must have a Task, and a Task must have a Parent Task - but not vice versa?)
 
Upvote 0
Thank you Eric , however in reality I know the Level 1 Tasks and the third column is also comma delimited . In the above example , i have the input where T0 and T5 both are level 1. Given this information, is it possible to enhance the code.
 
Upvote 0
It really doesn't make a lot of sense if columns 1 and 3 are comma delimited. If you have a task with 2 parents, you could have discrepancies. What if one parent is level 1 and the other is level 2? Then is the child level 2 or 3? If you want to find every conceivable path up and/or down the tree, that's a much larger task, requiring some kind of recursive algorithm. Would you want the highest level, or least? Or all?

Col has some good questions too. You might want to consider some project management software.
 
Upvote 0
I don't understand how you can have/allow orphan tasks.:confused:

I would expect that a Parent task is mandatory in the hierarchy and the very 1st level to be created, with subordinates (Tasks, Sub-tasks) only added where necessary for the work breakdown. So, as I indicated in my earlier post (#3), every Task and Sub-Task should always have an ultimate Parent task, whereas a Parent won't necessarily have children.

Just sayin' ;)
 
Upvote 0

Forum statistics

Threads
1,214,944
Messages
6,122,392
Members
449,081
Latest member
JAMES KECULAH

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