Try this macro
Sub Main()
Tree
Rearrange
End Sub
Sub Tree()
Dim FoundBoss As Boolean
Sheets(1).Activate
Sheets(2).Cells.Clear
For N = 2 To Cells(Rows.Count, 1).End(xlUp).Row
Cells(N, 1).Select
FoundBoss = True
Sheets(2).Cells(N, 12) = Cells(N, 1)
Sheets(2).Cells(N, 11) = Cells(N, 3)
CurrentSearch = Cells(N, 3)
Do While FoundBoss = True
FoundBoss = False
If Application.CountIf(Columns(1), CurrentSearch) > 0 Then
CurrentSearch = Columns(1).Find(CurrentSearch, , xlValues, xlWhole).Offset(0, 2)
FoundBoss = True
Sheets(2).Cells(N, 1).End(xlToRight).Offset(0, -1) = CurrentSearch
End If
Loop
Next N
End Sub
Sub Rearrange()
Sheets(2).Activate
For N = 2 To Cells(Rows.Count, 12).End(xlUp).Row
Range(Cells(N, 1), Cells(N, 1).End(xlToRight)).Delete Shift:=xlToLeft
Next N
LastRow = Cells(Rows.Count, 1).End(xlUp).Row
ActiveWorkbook.Worksheets("Sheet2").Sort.SortFields.Clear
For M = 1 To Cells(2, 1).CurrentRegion.Columns.Count
ActiveWorkbook.Worksheets("Sheet2").Sort.SortFields.Add Key:=Range(Cells(2, M), Cells(LastRow, M)) _
, SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
Next M
With ActiveWorkbook.Worksheets("Sheet2").Sort
.SetRange Cells(2, 1).CurrentRegion
.Header = xlNo
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
For M = 1 To Cells(2, 1).CurrentRegion.Columns.Count
For X = LastRow To 3 Step -1
If Cells(X, M) = Cells(X - 1, M) Then Cells(X, M).Clear
Next X
Next M
End Sub
It's not very efficient but it should get there in the end.
Bookmarks