VBA to find Hierarchy Low LEvel

DisturbingWorms

New Member
Joined
Jan 30, 2018
Messages
2
Hi All

In order to progress with some MRP calculations is need to find the lowest level in which a manufactured item is used within a BOM structure, column C below. The raw data below shows the relationships between the Parent and Child Items in columns A & B.

Items A, B and J return a low level of 0 as they are not child items in any of the relationships.
Items D & E are level 1 items as they are the children of level 0 items only.
Items F and G are level 2 items as they only feed into level 1 items ... etc etc.
Items N and O would be purchased items
Item K would have a low level of 4, although it feeds into E at level 2 its lowest level is where it feeds into H

I have a solution using repeated sorting to obtain the low levels for an item but I am looking for a VBA solution as the final list of relationships will be in excess of 4000. Depending on the sequence in which data is entered the actual list of relationships will not be sorted or grouped, relationships between pairs may occur anywhere in the list.

 

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
Sorry, forgot to add the sample data

Parent ItemChild ItemLow Level
AD0
AE0
BD0
BF0
JM0
DF1
DG1
FH2
GI2
HK3
KN4
KO4
EK1

<tbody>
</tbody><colgroup><col><col><col></colgroup>
 
Upvote 0
Cross posted
https://stackoverflow.com/questions/48465742/excel-bom-item-low-level
https://www.excelforum.com/excel-programming-vba-macros/1217544-vba-to-find-bom-low-level.html

While we do not prohibit Cross-Posting on this site, we do ask that you please mention you are doing so and provide links in each of the threads pointing to the other thread (see rule 13 here along with the explanation: Forum Rules). This way, other members can see what has already been done in regards to a question, and do not waste time working on a question that may already be answered.
 
Upvote 0
Cross posted
https://stackoverflow.com/questions/48465742/excel-bom-item-low-level
https://www.excelforum.com/excel-programming-vba-macros/1217544-vba-to-find-bom-low-level.html

While we do not prohibit Cross-Posting on this site, we do ask that you please mention you are doing so and provide links in each of the threads pointing to the other thread (see rule 13 here along with the explanation: Forum Rules). This way, other members can see what has already been done in regards to a question, and do not waste time working on a question that may already be answered.


Hello all,

I am sorry to dig out this threat but after reading all those links I see that the solution is not given. However I am really interested in an answer for that one. @Fluff what about cross posting but in another language? It is ok no? As long as we post the solution in those languages obviously :)
Thanks for your help
 
Upvote 0
Hello all,

I am sorry to dig out this threat but after reading all those links I see that the solution is not given. However I am really interested in an answer for that one. @Fluff what about cross posting but in another language? It is ok no? As long as we post the solution in those languages obviously :)
Thanks for your help


As I have no reply so far and I can understand as it is not my post, I took the liberty to ask a solution in another forum but in french and with different examples. (Here)
I will obviously come back here to post the solution like Fluff already said. And I will translate it if need be :)
 
Upvote 0
Hi All

In order to progress with some MRP calculations is need to find the lowest level in which a manufactured item is used within a BOM structure, column C below. The raw data below shows the relationships between the Parent and Child Items in columns A & B.

Items A, B and J return a low level of 0 as they are not child items in any of the relationships.
Items D & E are level 1 items as they are the children of level 0 items only.
Items F and G are level 2 items as they only feed into level 1 items ... etc etc.
Items N and O would be purchased items
Item K would have a low level of 4, although it feeds into E at level 2 its lowest level is where it feeds into H

I have a solution using repeated sorting to obtain the low levels for an item but I am looking for a VBA solution as the final list of relationships will be in excess of 4000. Depending on the sequence in which data is entered the actual list of relationships will not be sorted or grouped, relationships between pairs may occur anywhere in the list.


DisturbingWorms, did you find a solution for that one? Please share it.
 
Upvote 0
Guys here is a solution that works for me.
The parent in column A
Child in column B
All child must be unique and have only one parent.
Their can be many root parents.

Code:
Option Explicit
Dim plg


Public Function papa(ByVal nom As String) As String ' Function look for parents
Dim idf As Long     ' index data table
    For idf = 1 To UBound(plg)  ' if child found in column 2 then brings back parent
        If plg(idf, 2) = nom Then papa = plg(idf, 1): Exit Function
    Next idf
End Function
 
Public Sub generation()
Dim idx As Long     ' index data table
Dim ndp As String   ' name of parent
    With ActiveSheet                        ' Set up the table
        plg = .Cells(2, 1).Resize(.Cells(Rows.Count, 2).End(xlUp).Row - 1, 2).Value
        ReDim res(1 To UBound(plg))         ' store the table results
        For idx = 1 To UBound(plg)          ' loop data table
            res(idx) = 1                    ' initialize data result
            ndp = papa(plg(idx, 2))         ' search initial parent
            While ndp <> ""                 ' if parent find go to next
                res(idx) = res(idx) + 1     ' next level
                ndp = papa(ndp)             ' look for parent next level
            Wend
        Next idx                            ' show table with results
        .Cells(2, 4).Resize(UBound(res), 1).Value = Application.Transpose(res)
    End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,585
Messages
6,120,394
Members
448,957
Latest member
Hat4Life

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