+ Reply to Thread
Results 1 to 30 of 30

Recursive reverse BOM or Implosion

  1. #1
    Registered User
    Join Date
    11-16-2018
    Location
    georgia
    MS-Off Ver
    365
    Posts
    17

    Recursive reverse BOM or Implosion

    Hello everyone,

    I am new to the forum and have a problem imploding a bill of material. I want to put in a number and get the "where used" all the way to the top. The data is arranged by Parent, Child, Qty. I would like to have the output in a hierarchical format where the data may reach as many as 20 levels.

    I have included a file with a set of data and and 2 styles of example output (left side right side). If the data output is too difficult I guess I can rearrange data that has a straight list of child and parent.

    thank you in advance for your assistance
    Attached Files Attached Files

  2. #2
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: Recursive reverse BOM or Implosion

    Like this?

    A
    B
    C
    D
    E
    F
    G
    1
    Qty Component Parent Parent2 Parent3 Parent4 Parent5
    2
    1
    520/00011/20 520/00000/00 520/00000/MG 520/00000/PS
    3
    1
    520/33000/00 520/00000/00 520/00000/MG 520/00000/PS
    4
    1
    520/33010/00 520/00000/00 520/00000/MG 520/00000/PS
    5
    1
    520/42801/00 520/00000/00 520/00000/MG 520/00000/PS
    6
    1
    056/00000/00 520/00000/00 520/00000/MG 520/00000/PS
    7
    1
    520/00000/20 520/00000/00 520/00000/MG 520/00000/PS
    8
    1
    520/00000/00IQ 520/00000/00IQOQ
    9
    1
    520/00000/00OQ 520/00000/00IQOQ
    10
    1
    520/42870/03 520/00000/00IQOQ
    11
    1
    004/60074/08 520/00000/20 520/00000/00 520/00000/MG 520/00000/PS
    12
    1
    004/60074/09 520/00000/20 520/00000/00 520/00000/MG 520/00000/PS
    13
    1
    520/20800/99 520/00000/20 520/00000/00 520/00000/MG 520/00000/PS
    14
    1
    520/34000/00 520/00000/20 520/00000/00 520/00000/MG 520/00000/PS
    15
    1
    004/60074/08 520/00000/20X
    16
    1
    004/60074/09 520/00000/20X
    17
    1
    004/60074/17 520/00000/20X


    The formula in D2 is =IF(C2 = "", "", IFERROR(VLOOKUP(C2, B$2:C$4644, 2, FALSE), ""))
    Last edited by shg; 11-27-2018 at 01:41 PM.
    Entia non sunt multiplicanda sine necessitate

  3. #3
    Registered User
    Join Date
    11-16-2018
    Location
    georgia
    MS-Off Ver
    365
    Posts
    17

    Re: Recursive reverse BOM or Implosion

    Hello and thank you for the reply.

    I'm sorry I am not sure I follow.

    If I select the number 004/28136/00 then I would get something that would trail down like

    004/28136/00
    520/34007/04
    520/34007/01
    520/34007/00
    520/34017/01
    520/34017/00
    520/34000/00
    520/00000/20
    520/00000/00
    520/00000/MG
    520/00000/MG
    520/00000/PS
    520/00001/00
    520/00000/MG
    520/00000/PS
    520/00002/00
    520/00000/MG
    520/00000/PS
    520/00000/20X

    The true table is 500x of what it appears if that the entire file is imploded. correct?

  4. #4
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: Recursive reverse BOM or Implosion

    So if you find 004/28136/00 in the components column, ...

    A
    B
    C
    D
    E
    F
    G
    H
    I
    J
    K
    1
    Qty Component Parent Parent2 Parent3 Parent4 Parent5 Parent6 Parent7 Parent8 Parent9
    2285
    2
    004/28136/00 520/34007/04 520/34007/01 520/34007/00


    ... which agrees with your list as far as it goes. Not what you're looking for?
    Last edited by shg; 11-27-2018 at 02:29 PM.

  5. #5
    Registered User
    Join Date
    11-16-2018
    Location
    georgia
    MS-Off Ver
    365
    Posts
    17

    Re: Recursive reverse BOM or Implosion

    Hello, and thank you so far for your help.

    It is correct for one branch:

    004/28136/00 goes into 520/34007/04

    the remaining follows the picture

    Screenshot_3.png

    I am trying to think if your method could be addressed in another lookup table to find the other occurrences or if a vba procedure was needed to perform the backwards search.

  6. #6
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: Recursive reverse BOM or Implosion

    I'm sure whatever it it could be implemented in VBA, but I can't visualize what you're looking for.

    Maya a small, complete example (20 lines or so) with inputs and desired output?

  7. #7
    Registered User
    Join Date
    11-16-2018
    Location
    georgia
    MS-Off Ver
    365
    Posts
    17

    Re: Recursive reverse BOM or Implosion

    In the attached excel file is a tab call "results". It shows a pictorial of what I am looking for.

    Essentially, its the reverse of a indented bill of material. I want to start at the lowest level and make my way to the top. It becomes difficult when a component appears on more than one indented BOM.

    For example:

    004/28136/00 appears in 520/34007/04
    Then you move to the next upward step
    520/34007/04 appears in 2 different bom's 520/34007/01 and 520/34017/01
    Taking the first item we move upward
    520/34007/01 appears in 520/34007/00 and we stop with this branch.
    Now we go back to the other branch 520/34017/01
    520/34017/01 appears in 520/34017/00
    Then the next step upward
    520/34017/00 appears in 520/34000/00
    and so on

    Does this help? and thank you

  8. #8
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: Recursive reverse BOM or Implosion

    Missed that, but now that I see it, I don't understand it. Why is it disjoint, with col K blank?

    What information does it usefully convey?

  9. #9
    Registered User
    Join Date
    11-16-2018
    Location
    georgia
    MS-Off Ver
    365
    Posts
    17

    Re: Recursive reverse BOM or Implosion

    Hello,

    Sorry about the picture the tab shows two configurations of the same material.

    Version 1 and version 2 depict the same output. Version 2 is moving directly to the right as you move to the next level.

    Version 1
    version1.png

    Version 2
    version2.png

    The reason I am doing this is to find the assemblies and higher assemblies for a product change. When an engineer makes a change to a lower part of an assembly you need to look for all the assemblies that are effected by the change. Those assemblies may be contain within other assemblies and or finished products. Eventually I want to find what things are in a warehouse that would need to be modified or out a customer's site that would need to be recalled.

    I hope this helps. Thanks

  10. #10
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: Recursive reverse BOM or Implosion

    Slow on the uptake, apparently.

    If you filter for a given component, doesn't this tell you all the affected higher-level assemblies?

    A
    B
    C
    D
    E
    F
    G
    H
    1
    Qty Component Parent1 Parent2 Parent3 Parent4 Parent5 Parent6
    2285
    2
    004/28136/00 520/34007/04 520/34007/01 520/34007/00
    2286
    2
    004/28136/00 520/34057/04 520/34057/00 520/33610/00 520/00000/SSRA 520/00000/PS
    Last edited by shg; 11-27-2018 at 07:59 PM.

  11. #11
    Registered User
    Join Date
    11-16-2018
    Location
    georgia
    MS-Off Ver
    365
    Posts
    17

    Re: Recursive reverse BOM or Implosion

    Hello,

    Sorry for confusion. The issue become the permutations of the other components to the right of the first column. In your format the correct result would be the following:

    version3.png

    If you filtered to each of the columns with the values of the cell to the left of the you would get the all the items. You may also get additional items as some may be used on other levels but just not associated with the 004/28136/00 item. Does this make sense?

  12. #12
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: Recursive reverse BOM or Implosion

    I think the penny just dropped; it's a one-to-many relationship at all levels of the hierarchy.

    Hove to ponder what is an appropriate data structure.

  13. #13
    Registered User
    Join Date
    11-16-2018
    Location
    georgia
    MS-Off Ver
    365
    Posts
    17

    Re: Recursive reverse BOM or Implosion

    Hello,

    I started looking at the problem as a recursive loop and assigning a hierarchy (outline)level to the results from the table and came up with the following list. I am wondering if I can just assign an array position to this location.

    So starting at the child level 004/28136/00 (1.0) I get to 2 hits [520/34007/04, 520/34057/04] becoming (1.1 and 1.2) adding them to a list.

    Take the first item 520/34007/04 (1.1) I get 2 hits becoming 1.1.1 and 1.12 append them to the list.

    Take the next unchecked item 520/34057/04 (1.2) giving 1 hit [520/34057/00] becoming 1.2.1 and append this to the list

    so on and so forth.

    I manually did this and ended up after sorting with (see hiearchy tab) The green is in a location format


    Trying to think how to handle an unknown size of an array - maybe append to a txt file. not sure about coding this?
    Attached Files Attached Files

  14. #14
    Forum Guru karedog's Avatar
    Join Date
    10-03-2014
    Location
    Indonesia
    MS-Off Ver
    2003
    Posts
    2,971

    Re: Recursive reverse BOM or Implosion

    Please Login or Register  to view this content.
    Last edited by karedog; 11-30-2018 at 04:04 AM.
    1. I care dog
    2. I am a loop maniac
    3. Forum rules link : Click here
    3.33. Don't forget to mark the thread as solved, this is important

  15. #15
    Registered User
    Join Date
    11-16-2018
    Location
    georgia
    MS-Off Ver
    365
    Posts
    17

    Re: Recursive reverse BOM or Implosion

    Hello, and thank you for the code. I am not very experienced. I pasted the code in "bom" code page and ran. It runs for quite a while then I get "Not Responding" How does the code pick up the value to search?

  16. #16
    Registered User
    Join Date
    11-16-2018
    Location
    georgia
    MS-Off Ver
    365
    Posts
    17

    Re: Recursive reverse BOM or Implosion

    Hello karedog

    I understand what has happened. The original file was a reduced version of data table. The true size is about 500x larger and excel has run out of room. I am amazed at the coding and the results and what you were able to accomplish. Is there a way to ask for a starting number either by dialog or pick up the value of a cell as the starting point.

    Again thank you for your time, much appreciated.

  17. #17
    Forum Guru karedog's Avatar
    Join Date
    10-03-2014
    Location
    Indonesia
    MS-Off Ver
    2003
    Posts
    2,971

    Re: Recursive reverse BOM or Implosion

    You are welcome, micro62w.

    I have modified the code, so it will be more "resources friendly" (like RAM usage etc), please test it on your big file to see if there is some improvement.

    As per your request, now the code will search the value of cell bom!E2 (Sheet "bom" at cell E2) :
    - If this cell is empty, then the code will search for all items
    - If this cell is not empty, but the code cannot find any parent that match this cell's value, a messagebox will popped up as a warning, and the macro will stop
    - If this cell is not empty and the code can find a parent with this cell's value, then query is performed for this parent only

    Please Login or Register  to view this content.

  18. #18
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: Recursive reverse BOM or Implosion

    kd, re your PM,

    My formula doesn't solve the problem. It was one I created to make a tabular org chart starting from a two column list of {employee, supervisor}, and it assumed by definition that each child has exactly one parent, other than the top dog, who has none.

    C
    D
    E
    F
    G
    H
    I
    J
    K
    2
    ID
    Spon ID
    Spon ID
    Spon ID
    Spon ID
    Spon ID
    Spon ID
    Spon ID
    Spon ID
    3
    050180
    4
    050226 050180
    5
    052322 050226 050180
    6
    053075 050226 050180
    7
    071009 050226 050180
    8
    081168 071009 050226 050180
    9
    117995 081168 071009 050226 050180
    10
    323848 117995 081168 071009 050226 050180
    11
    323991 117995 081168 071009 050226 050180
    12
    130637 081168 071009 050226 050180
    13
    326896 130637 081168 071009 050226 050180
    14
    340248 130637 081168 071009 050226 050180
    15
    401300 340248 130637 081168 071009 050226 050180


    That's not the case here, when any given assembly ("child", down to the component level) can be used in multiple higher-level assemblies ("parent"). I did not read your code closely, but this kind of data structure

    Please Login or Register  to view this content.
    ... was what I was trying to get my brain moving toward.

    Nice work.

  19. #19
    Registered User
    Join Date
    11-16-2018
    Location
    georgia
    MS-Off Ver
    365
    Posts
    17

    Re: Recursive reverse BOM or Implosion

    Hello Karedog,

    Thank you for the work. The individual item works wonderfully!!! The full set of data is 900k items and I get an "out of stack space (error 28)" statement. I reduced to 100k and still get the error. The result may stretch beyond excels limits. Can the code be carried over the "access"?

    Additionally, I am wondering to carry the quantity and or u_m columns along do I continue to add to the existing code or is it best to supplement the results afterward.

    again I can't tell you how much I appreciate your assistance.

  20. #20
    Forum Guru karedog's Avatar
    Join Date
    10-03-2014
    Location
    Indonesia
    MS-Off Ver
    2003
    Posts
    2,971

    Re: Recursive reverse BOM or Implosion

    shg,

    Thanks. Still I don't believe there is someone can think about this just using formulas




    micro62w,

    >>> I get an "out of stack space (error 28)" statement
    Oops, that is easy to spot (based on the error code), I made a mistake in the quicksort routine.
    I have updated my code on post #14, please try it again.

    >>> The result may stretch beyond excels limits
    On the updated code on post #14, I change the code so the output will come in several seperated sheets.
    So for example if the output take 2,097,160 rows, the output will be displayed in these sheets :
    - MyResults_1 (1,048,576 rows)
    - MyResults_2 (1,048,576 rows)
    - MyResults_3 (8 rows)

    As for testing purpose, if using your file on post #1, and :
    - change the value of cell bom!E2 to 004/28136/00
    - edit this line of code :
    Please Login or Register  to view this content.
    to
    Please Login or Register  to view this content.
    The macro will produce 4 sheets (MyResults_1 to MyResults_4)


    >>> Can the code be carried over the "access" ?
    Maybe, but I am not familiar with "Access", and I can imagine it will be extremely painfully very slow, since the process is performed based on file, not based on memory.
    And beside, even if this could be done, how will you display the result then ?
    But I guess using my edited code above (the output is seperated into several sheets), it will suite your need ?


    >>> Additionally, I am wondering to carry the quantity and or u_m columns along do I continue to add to the existing code or is it best to supplement the results afterward.
    If the problems above have been solved, then we can continue to this your next request (but not before solved).
    If so, then upload your workbook with desired layout result.

  21. #21
    Forum Guru karedog's Avatar
    Join Date
    10-03-2014
    Location
    Indonesia
    MS-Off Ver
    2003
    Posts
    2,971

    Re: Recursive reverse BOM or Implosion

    One more thing, I need to know your Excel version (32 bit or 64 bit), and the amount of RAM on your PC (the code will be made based on this).

  22. #22
    Registered User
    Join Date
    11-16-2018
    Location
    georgia
    MS-Off Ver
    365
    Posts
    17

    Re: Recursive reverse BOM or Implosion

    Hello Karedog

    Everything seems to be working great - again thank you.

    Using excel 2016 - 32bit with 8 gb ram

    Part of this for me is to learn - A lot of what you are doing I do not understand could you possible notate the reason for the loops, would give me a start on understanding.

    I have included what I would be including next which is getting info from other tables and or columns and adding it to the structure. I added the table and put in 2 possible displays. The later is different on the placement I was curious if you thought this version was a more readable format or was more program friendly.

    Also once I understand (hopefully) I would like to add the reversal of extracting a BOM from top down (1 or many levels) and also flattening the BOM and adding up the qty used. I have seen many examples for the later solutions but I want to keep the same technique.

  23. #23
    Forum Guru karedog's Avatar
    Join Date
    10-03-2014
    Location
    Indonesia
    MS-Off Ver
    2003
    Posts
    2,971

    Re: Recursive reverse BOM or Implosion

    You are welcome.


    I have included what I would be ...
    Where is it ?



    This is the explanation :

    Read data from sheet bom :
    Please Login or Register  to view this content.


    Since I thought the memory usage is tight in this case, so I convert the variant array (where each item of its member take 16 bytes, excluding the string itself) to string array (each member only take 4 bytes). I read column by column (column A then column B) to variant array b, which then using looping, writing to string array a. We can save about 900k x 2 columns x (16 - 4 bytes) = 21,600,000 bytes, or about 20 MB of RAM this way.
    Please Login or Register  to view this content.


    Next step is, for each unique parent item in column B, we will write down the row number where itself and it's duplicates occurred.
    For get the unique parent items, we will use Excel's collection object (z1 in this case), and since the collection cannot change its data directly, we will use array b to store the collection's data.
    For example, take item 520/00011/20, this item are listed in these rows : row 1, row 48, row 223 (we exclude the header so row 2 in the cell is row 1 in array, row 49 in in cell is row 48 in array etc). So the value of array b(1) is "1,48,223" where all of item 520/00011/20 is occurred. In other word, array b will be act as a holder of children list.
    Please Login or Register  to view this content.


    Now, we need to get all the topmost parent items (a node that has no parent, or the "Top Dog" as shg said).
    If the value of cell bom!E2 is not empty, then we only search for this parent item only, otherwise all parent items is calculated.
    We add these topmost parent items to second collection (z2). This z2 will act as a "joblist" that should be calculated.
    When you said "Recursive reverse BOM or Implosion", you are right, this kind of job must be performed using recursive method, but I will use collection object as a holder of a stack in this case, so the recursive can be avoided.
    Please Login or Register  to view this content.


    As we know that at current state, the value of array b is a string (for example above, it is like "1,48,223"). Now we will convert/split this string into individual array member.
    Please Login or Register  to view this content.


    We also want the array is sorted, for example if the value are 520/34017/01 and 520/34007/01, we will sort them into 520/34007/01 520/34017/01
    Please Login or Register  to view this content.


    We write back the array (from the splitted string) into array b (so array b now is converted from a string array to a jagged array of Long):
    Please Login or Register  to view this content.


    As the code above, we put the unique parent items of column B into collection z2 by these lines :
    Please Login or Register  to view this content.
    but these items are not really the most "Top Dog" items. For example on cell bom!B3077 (520/34007/04), if we look cell bom!A3108, this "parent" has another parent (003/21064/00), so this is not an actual "Top Dog", we must delete this from collection z2, by using this code :
    Please Login or Register  to view this content.


    Now we already have the "Top Dogs", but these "Top Dogs" are not sorted yet (remember, you have another sort routine above, but that is for the "children", not for the Top Dogs).
    Since you have big data (900k), we will use the fastest sort routine : QuickSort. In the next lines, we prepare the Quicksort by writing collection z2 to array v1, then sort it, then read back to collection z2.
    Please Login or Register  to view this content.


    At this point, all the preparations is completed, and we ready the actual writing to the cell.
    Please Login or Register  to view this content.


    Some preparations (delete old output sheets if they are exists, create a new one, etc :
    Please Login or Register  to view this content.


    Now we do the "recursive", the job is listed on collection z2, we will proceed one by one the items of z2, everytime an item has been processed, we delete this item from the collection z2, so if there is no more item remained on z2 (z2.count = 0), it means all the job has been calculated and we can exit the Do..Loop.
    Please Login or Register  to view this content.

  24. #24
    Registered User
    Join Date
    11-16-2018
    Location
    georgia
    MS-Off Ver
    365
    Posts
    17

    Re: Recursive reverse BOM or Implosion

    Hello,

    Thanks for the explanation very helpful. It will take some studying. Sorry I missed attachment.

    Here you go:

    I just realized it failed due to the size.
    Attached Files Attached Files

  25. #25
    Forum Guru karedog's Avatar
    Join Date
    10-03-2014
    Location
    Indonesia
    MS-Off Ver
    2003
    Posts
    2,971

    Re: Recursive reverse BOM or Implosion

    How do you calculate sheet Result1 column M (qty from bom) ?

    For example, take cell Result1!C4 (the item is 520/34007/01).

    If you AutoFilter sheet bom column A with this item (520/34007/01), Excel will return :
    Please Login or Register  to view this content.
    If you sum column C of this filtered range, the sum is 74, but on your manual calculation on cell Result1!M4, it is written 1

    Also note that the unit is not always the same, for example for the filtered range above, you got one item with "IN" unit while the others is in "EA" unit.

  26. #26
    Registered User
    Join Date
    11-16-2018
    Location
    georgia
    MS-Off Ver
    365
    Posts
    17

    Re: Recursive reverse BOM or Implosion

    Hello,

    The qty in column M is the number of times an item is in the parent item. 520/34007/01 is a component of 520/34007/00 qty(1) or 004/28136/00 is in 520/34007/04 qty 2. Perhaps it would be clearer for users if it were shifted 1 row higher.

    Thank you

  27. #27
    Forum Guru karedog's Avatar
    Join Date
    10-03-2014
    Location
    Indonesia
    MS-Off Ver
    2003
    Posts
    2,971

    Re: Recursive reverse BOM or Implosion

    Below is the code as your request :
    - Sub Test3_Layout1 will create output as sample layout in your sheet Result1
    - Sub Test3_Layout2 will create output as sample layout in your sheet Result2

    Please Login or Register  to view this content.

  28. #28
    Registered User
    Join Date
    11-16-2018
    Location
    georgia
    MS-Off Ver
    365
    Posts
    17

    Re: Recursive reverse BOM or Implosion

    Karedog - Thank you for everything.

  29. #29
    Forum Guru karedog's Avatar
    Join Date
    10-03-2014
    Location
    Indonesia
    MS-Off Ver
    2003
    Posts
    2,971

    Re: Recursive reverse BOM or Implosion

    You are welcome.

    Don't forget to mark this thread as solved :
    select Thread Tools from the menu link above and mark this thread as SOLVED.

    Regards

  30. #30
    Registered User
    Join Date
    11-16-2018
    Location
    georgia
    MS-Off Ver
    365
    Posts
    17

    Re: Recursive reverse BOM or Implosion

    Hi Karadog,

    Some time ago you helped me with a problem on recursive boms. I have changed the data set and I am experiencing an error "subscript out of range". I have included a table on the bom-data tab to the right that details items that yield the error. I would appreciate any help.

    Since the file is too big I am trying to send data set and code for the file.

    Thank you so much

    The error appears to occur here ( ReDim v1(1 To z2.Count, 1 To 2) 'Quick sort the uppermost items')


    Option Explicit
    Private Sub QS(ByRef v1, iL As Long, iH As Long)
    Dim pvt As String, tmp As String, tL As Long, tH As Long
    tL = iL: tH = iH: pvt = v1((iL + iH) \ 2, 1)
    While tL <= tH
    While v1(tL, 1) < pvt And tL < iH: tL = tL + 1: Wend
    While pvt < v1(tH, 1) And tH > iL: tH = tH - 1: Wend
    If (tL <= tH) Then
    tmp = v1(tL, 1): v1(tL, 1) = v1(tH, 1): v1(tH, 1) = tmp
    tmp = v1(tL, 2): v1(tL, 2) = v1(tH, 2): v1(tH, 2) = tmp
    tL = tL + 1: tH = tH - 1
    End If
    Wend
    If iL < tH Then QS v1, iL, tH
    If tL < iH Then QS v1, tL, iH
    End Sub

    Public Sub Implosion()
    Const prefixSheetName As String = "Structure_"

    Dim a() As String, b, c() As Long, e As Long, i As Long, j As Long, k As Long, pRow As Long, pCol As Long, maxRow As Long, maxCol As Long, rng As Range, sht As Worksheet, strToSearch As String, v1, v2, z1 As New Collection, z2 As New Collection
    Dim Pcol2 As Long, Pcol3 As Long, Pcol4 As Long, Pcol5 As Long, Pcol6 As Long, Pcol7 As Long, Pcol8 As Long, Pcol9 As Long, totalCells As Long, r As Long, plast As Long, lrow As Long, lcol As Long


    Debug.Print Format$(Now, "HH:MM:SS")

    With Sheets("bom-data") 'Read data from sheet "BOM"
    Set rng = .Range("A1").CurrentRegion.Offset(1).Resize(, 2)
    Set rng = rng.Resize(rng.Rows.Count - 1)
    strToSearch = .Range("F2").Value
    End With

    '======
    ReDim a(1 To rng.Rows.Count, 1 To 2) 'Memory consolidation
    For j = 1 To 2
    b = rng.Columns(j).Value
    For i = 1 To UBound(b, 1)
    a(i, j) = b(i, 1)
    Next i
    Next j

    ReDim b(1 To UBound(a, 1)) ' Collect the unique items in column B along with each of its occurances
    For i = 1 To UBound(a, 1)
    On Error Resume Next
    z1.Add Key:=a(i, 2), item:=i
    On Error GoTo 0
    j = z1(a(i, 2))
    If Len(b(j)) = 0 Then b(j) = i Else b(j) = b(j) & "," & i
    Next i


    'Recursive execution
    If Len(strToSearch) Then '--> cell bom!E2 is not empty, which means we will try to calculate only for this specific parent
    On Error Resume Next
    i = z1(strToSearch)
    If Err.Number = 5 Then MsgBox "Item " & strToSearch & " is not found , Possible upper Assy!": Exit Sub '--> If this parent is not found, then end macro immediately
    On Error GoTo 0
    z2.Add Key:=strToSearch, item:=i '--> this parent item is found, so add it to the z2 collection
    Else
    For Each v1 In z1 '--> since cell bom!E2 is empty, we will calculate all the "Top Dogs", so add everything from z1 (unique parents in column B) to z2
    z2.Add Key:=a(v1, 2), item:=v1
    Next v1
    End If


    For i = 1 To UBound(b) 'Split the array
    If Len(b(i)) Then
    v1 = Split(b(i), ",")
    ReDim c(1 To UBound(v1) + 1)

    For j = 1 To UBound(c)
    c(j) = CLng(v1(j - 1))
    Next j

    For j = 1 To UBound(c) 'Sort the above array
    For k = j + 1 To UBound(c)
    If a(c(k), 1) < a(c(j), 1) Then e = c(j): c(j) = c(k): c(k) = e
    Next k
    Next j

    b(i) = c 'write the array back as jagged array of long


    On Error Resume Next 'Get the uppermost bom item add delete the false ones
    For j = 1 To UBound(c)
    z2.Remove a(c(j), 1)
    Next j
    On Error GoTo 0
    End If
    Next i

    '**************************************************************************
    MsgBox ("here")
    ReDim v1(1 To z2.Count, 1 To 2) 'Quick sort the uppermost items'
    'modified this to z2.count+1 it was z2.count, and got out of subscript range

    i = 0
    For Each v2 In z2
    i = i + 1
    v1(i, 1) = a(v2, 2)
    v1(i, 2) = v2
    Next v2

    Set z2 = Nothing
    QS v1, 1, UBound(v1, 1)
    For i = 1 To UBound(v1, 1)
    z2.Add Array(v1(i, 2), 1)
    Next i
    'data is complete


    '****************************************************************************


    Debug.Print Format$(Now, "HH:MM:SS") 'Start the data writing process.

    Application.ScreenUpdating = False 'Delete old sheets and create new one start data write at Row 1
    maxRow = Sheets(1).Rows.Count '10
    maxCol = Sheets(1).Columns.Count
    Application.DisplayAlerts = False
    For Each sht In Worksheets
    If Left$(sht.Name, Len(prefixSheetName)) = prefixSheetName Then sht.Delete
    Next sht
    Application.DisplayAlerts = True
    Set sht = Sheets.Add(after:=Sheets(Sheets.Count))
    sht.Name = prefixSheetName & "1"

    sht.Cells(1, 1) = "Reverse Levels for Implosion "
    sht.Cells(2, 1) = "1": sht.Cells(2, 2) = "2": sht.Cells(2, 3) = "3": sht.Cells(2, 4) = "4": sht.Cells(2, 5) = "5"
    sht.Cells(2, 6) = "6": sht.Cells(2, 7) = "7": sht.Cells(2, 8) = "8": sht.Cells(2, 9) = "9"
    sht.Cells(2, 10) = "Part Numbers "
    Sheets(sht.Name).Cells.Select
    Selection.NumberFormat = "@"
    Selection.VerticalAlignment = xlCenter
    Selection.HorizontalAlignment = xlCenter
    Sheets(sht.Name).Columns("A:I").ColumnWidth = 3.57
    Sheets(sht.Name).Columns("J").ColumnWidth = 18
    sht.Cells(1, 1).Select

    'Initialize the column count
    pRow = 2 'move down for future items
    Pcol2 = 1: Pcol3 = 1: Pcol4 = 1: Pcol5 = 1: Pcol6 = 1: Pcol7 = 1: Pcol8 = 1: Pcol9 = 1: plast = 1
    '-------------------------

    Do

    v1 = z2(1) 'Retrieve first item (job) from collection z2
    z2.Remove 1 'Since it has been saved to variable v1, we can remove it from the collection (so the next remaining item will become the next first item in the next loop)
    pRow = pRow + 1 'Increment pRow (pRow is a variable that point at what row the data will be written)
    pCol = v1(1) 'Get pCol (pCol is a variable that point at what column the data will be written)
    If pCol > maxCol Then MsgBox "Not enough columns to display": Exit Sub 'If pCol is greater than the maximum number of columns that Excel has, then write a MessageBox and quit
    If pRow > maxRow Then 'If pRow is exceeding the maximum number of rows that Excel has, then create a new sheet and all next jobs will be performed on this new sheet
    i = CLng(Split(sht.Name, "_")(1)) + 1
    Set sht = Sheets.Add(after:=Sheets(Sheets.Count))
    sht.Name = prefixSheetName & i
    pRow = 1
    End If

    'Write data to cell
    If pRow = 3 Then
    GoTo jump
    End If
    If plast >= pCol Then
    'sht.Cells(pRow - 1, 11) = "*"
    sht.Cells(pRow - 1, 10).Interior.ColorIndex = 35
    End If
    jump:
    If pCol = 1 Then
    sht.Cells(pRow, pCol) = pCol
    sht.Cells(pRow, 10) = a(v1(0), 2)
    v2 = b(v1(0))
    Else
    ' Check for clomn item and number appropriatly
    If pCol = 2 Then
    sht.Cells(pRow, pCol) = Pcol2
    Pcol2 = Pcol2 + 1
    Pcol3 = 1: Pcol4 = 1: Pcol5 = 1: Pcol6 = 1: Pcol7 = 1: Pcol8 = 1: Pcol9 = 1
    End If
    If pCol = 3 Then
    sht.Cells(pRow, pCol) = Pcol3
    Pcol3 = Pcol3 + 1
    Pcol4 = 1: Pcol5 = 1: Pcol6 = 1: Pcol7 = 1: Pcol8 = 1: Pcol9 = 1
    End If
    If pCol = 4 Then
    sht.Cells(pRow, pCol) = Pcol4
    Pcol4 = Pcol4 + 1
    Pcol5 = 1: Pcol6 = 1: Pcol7 = 1: Pcol8 = 1: Pcol9 = 1
    End If
    If pCol = 5 Then
    sht.Cells(pRow, pCol) = Pcol5
    Pcol5 = Pcol5 + 1
    Pcol6 = 1: Pcol7 = 1: Pcol8 = 1: Pcol9 = 1
    End If
    If pCol = 6 Then
    sht.Cells(pRow, pCol) = Pcol6
    Pcol6 = Pcol6 + 1
    Pcol7 = 1: Pcol8 = 1: Pcol9 = 1
    End If
    If pCol = 7 Then
    sht.Cells(pRow, pCol) = Pcol7
    Pcol7 = Pcol7 + 1
    Pcol8 = 1: Pcol9 = 1
    End If
    If pCol = 8 Then
    sht.Cells(pRow, pCol) = Pcol8
    Pcol8 = Pcol8 + 1
    Pcol9 = 1
    End If
    plast = pCol
    sht.Cells(pRow, 10) = a(v1(0), 1)
    v2 = Empty
    On Error Resume Next
    v2 = b(z1(a(v1(0), 1)))
    On Error GoTo 0
    End If
    '--------------------

    If IsArray(v2) Then 'If this item has a child, we will "recurse" into the childs, by pushing/adding all the childs to the collection z2

    For i = UBound(v2) To 1 Step -1
    If z2.Count Then
    z2.Add item:=Array(v2(i), pCol + 1), before:=1
    Else
    z2.Add item:=Array(v2(i), pCol + 1)
    End If
    Next i
    End If
    Loop Until z2.Count = 0
    sht.Cells(pRow, 10).Interior.ColorIndex = 35

    Application.ScreenUpdating = True
    Debug.Print Format$(Now, "HH:MM:SS") & vbCrLf

    '------ insert Item numbers

    Sheets(sht.Name).Range("J3").Select: Sheets(sht.Name).Range(Selection, Selection.End(xlDown)).Select
    totalCells = Selection.Cells.Count
    Sheets(sht.Name).Columns("A:A").Select: Sheets(sht.Name).Range("A2").Activate: Selection.Insert Shift:=xlToRight
    Sheets(sht.Name).Range("A3").Select
    For r = 1 To totalCells
    ActiveCell.Value = r
    ' ActiveCell.Interior.ColorIndex = r
    ActiveCell.Offset(1, 0).Select
    Next r
    sht.Cells(3, 11).Interior.ColorIndex = 24
    Sheets(sht.Name).Range("B1:J1").Merge
    sht.Cells(2, 1) = "Item"
    Sheets(sht.Name).Range("B1:J1").BorderAround ColorIndex:=1
    Sheets(sht.Name).Range("A2:K2").Borders.Weight = xlThin
    Sheets(sht.Name).Range("K3").Select

    'Call Mktbl

    'Call UpDate_Tbl

    End Sub
    Attached Files Attached Files

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Reverse strings udf function to reverse numbers
    By YasserKhalil in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 03-07-2015, 07:03 AM
  2. [SOLVED] Help with Recursive VBA
    By tddavid89 in forum Excel General
    Replies: 2
    Last Post: 11-08-2012, 09:42 PM
  3. Help with Recursive Call?
    By mark in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 05-25-2006, 03:50 PM
  4. [SOLVED] recursive sums
    By JMB in forum Excel Formulas & Functions
    Replies: 24
    Last Post: 09-06-2005, 03:05 PM
  5. recursive sums
    By JMB in forum Excel Formulas & Functions
    Replies: 18
    Last Post: 09-06-2005, 04:05 AM
  6. [SOLVED] recursive sums
    By Joe in forum Excel Formulas & Functions
    Replies: 0
    Last Post: 09-06-2005, 12:05 AM
  7. recursive sums
    By Joe in forum Excel Formulas & Functions
    Replies: 0
    Last Post: 09-05-2005, 11:05 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1