Make a formula to design a hierarchy from a parent child relation

jbesclapez

Active Member
Joined
Feb 6, 2010
Messages
275
Hello,

I get regularly files with parent-child hierarchy in a unsorted way. My task it to first, sort it the way it should be sorted and then on another place make a graphical hieararchy in excel as it is easier to read.
I would like to have a formula that helps me do this. The formula would select the range for parent, then select the range for child, then select a cell where I can copy paste the sorted result of the hierarchy and another cell that would would display the graphical hierarchy. The formula could be called ParentChild and then would look like this
=ParentChild(A6:A10;C6:C10,F5,P2)
A6:A10 are the array of the parent
C6:C10 are the array of the parent and an error should pop up if the array as a different size (each child as a parent except the member Root)
F5 is the place were we will sort a clean hierarchy. Note that the F5 to Fx will be for the parent when G5 to Gx will be for the Child. Optional :This array should have no data before.
P2 is the place where the array will be paste. So if the hiearchy is 3 level deep than the hierarchy will be shown in a array from P2 to Sx - As P is the root, Q is level 1, R is level 2, S is level 3. Optional :This array should have no data before.
Note:The value in the formula are not static! It is not always A6:A10 for example!


Note that the depth of the hierarchy can change and is not know before.

So, in real life you get this file:

PizzaMargerita
RootHotDog
RootPizza
MargeritaCheese
PizzaSea
SeaTuna
BurgerBun
HotDogSausage
SeaShrimp
RootSandwich
SandwichBurger
BurgerSteack
BurgerTomato
BurgerCucumber
HotDogBun
RootOffer
OfferPizza
OfferSandwich
MargeritaTomato
MargeritaOliva
SeaTomato
SeaCheese


Then it gets nicely sorted like this :


RootPizza
PizzaMargerita
MargeritaTomato
MargeritaOliva
MargeritaCheese
PizzaSea
SeaTuna
SeaShrimp
SeaTomato
SeaCheese
RootSandwich
SandwichBurger
BurgerSteack
BurgerBun
BurgerTomato
BurgerCucumber
RootHotDog
HotDogSausage
HotDogBun
RootOffer
OfferPizza
OfferSandwich

And it creates the hierarchy like this : Optional: It should display graphically in excel like in the picture attached.
RootPizza
Margerita
Tomato
Oliva
Cheese
Sea
Tuna
Shrimp
Tomato
Cheese
Sandwich
Burger
Steack
Bun
Tomato
Cucumber
HotDog
Sausage
Bun
Offer
Pizza
Sandwich

Thanks for your time and effort,

Have a good day,
 

Attachments

  • 2020-05-08_12-36-49.png
    2020-05-08_12-36-49.png
    8.7 KB · Views: 231

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
I forgot to add that a child can have n Parent and therefore be in many hiearchies... Mike?Fluff? Please? This is to high level for me as I am sure you will be using dictionnaries no?
 
Upvote 0
OK, I found this VBA that nearly does the job but it needs changes. In this VB the child is in column A and the Parent in B. And it is not a formula and does not ask for any place where to store the data...
WOrkign on it but help would be appreciated


VBA Code:
Dim n, ligne, debOrg, Tbl()
Sub organigramme()
  Tbl = Range("A2:B" & [A65000].End(xlUp).Row).Value
  Set debOrg = [d8]
  debOrg.Resize(25, 25).Clear
  n = UBound(Tbl)
  ligne = 0: Ecrit Tbl(1, 1), 1
  ligne = 0: Présentation Tbl(1, 1), 1
End Sub
Sub Ecrit(parent, niv)       ' procédure récursive
  ligne = ligne + 1
  debOrg.Offset(ligne, niv) = parent
  debOrg.Offset(ligne, niv).Borders(xlEdgeLeft).Weight = xlThin
  debOrg.Offset(ligne, niv).Borders(xlEdgeBottom).Weight = xlThin
  For i = 1 To n
    If Tbl(i, 2) = parent Then Ecrit Tbl(i, 1), niv + 1
  Next i
End Sub
Sub Présentation(parent, niv) ' procédure récursive
  ligne = ligne + 1
  Fin = debOrg.Offset(ligne, niv).End(xlDown).Row
  If Fin < 100 Then
    For i = ligne To Fin - debOrg.Row
       debOrg.Offset(i, niv).Borders(xlEdgeLeft).Weight = xlThin
    Next i
  End If
  For i = 1 To n
    If Tbl(i, 2) = parent Then Présentation Tbl(i, 1), niv + 1
  Next i
End Sub
 
Upvote 0
also struggling with this exact issue, any luck?
in the way I'm viewing it's the same principle, except I have an employee name and a manager name, I want to create a hierarchy of this data
 
Upvote 0
I am struggling a lot with this. Did you happen to find the VBA code corrections you could share? Or an example spreadsheet would be even better if available?
 
Upvote 0

Forum statistics

Threads
1,214,833
Messages
6,121,865
Members
449,052
Latest member
Fuddy_Duddy

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