Create Parent Child relationship in Excel

subhojyoti

New Member
Joined
Nov 12, 2018
Messages
3
Hi,
I'm new to Excel Scripting.
I need help to create below parent child relationship in excel. I have the source file in below format.
Level1 Level2Level3Level4
PC_BMROACD_KENBM24158009
PC_BMROACD_KENBM24158010
PC_BMROACD_KENBM24158011
PC_BMROACD_KENBM24158012
PC_BMROACD_KENBM24158013
PC_BMROACD_KENBM24158014
PC_BMROACD_KENBM24158015
PC_BMROACD_KENBM24158072

<colgroup><col span="4"></colgroup><tbody>
</tbody>

And my output needs to be like this
ParentChild
PC_BMROACD_KENBM
CD_KENBM24158
24158009
24158010
24158011
24158012
24158013
24158014
24158015
24158072

<colgroup><col span="2"></colgroup><tbody>
</tbody>




Any help is appreciated. The number of rows will be dynamic but column can remain the same.

<colgroup><col><col></colgroup><tbody>
</tbody>
 

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.
Try this for results in columns "F & G".
Code:
[COLOR="Navy"]Sub[/COLOR] MG12Nov56
[COLOR="Navy"]Dim[/COLOR] Rng [COLOR="Navy"]As[/COLOR] Range, Dn [COLOR="Navy"]As[/COLOR] Range, n [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Long,[/COLOR] Ray [COLOR="Navy"]As[/COLOR] Variant, ac [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Long[/COLOR]
[COLOR="Navy"]Dim[/COLOR] Dic [COLOR="Navy"]As[/COLOR] Object, K [COLOR="Navy"]As[/COLOR] Variant, P [COLOR="Navy"]As[/COLOR] Variant
[COLOR="Navy"]Dim[/COLOR] c [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Long[/COLOR]

Ray = Range("A1").CurrentRegion
[COLOR="Navy"]Set[/COLOR] Dic = CreateObject("scripting.dictionary")
Dic.CompareMode = vbTextCompare


[COLOR="Navy"]For[/COLOR] ac = 1 To UBound(Ray, 2) - 1
    [COLOR="Navy"]For[/COLOR] n = 2 To UBound(Ray, 1)
        [COLOR="Navy"]If[/COLOR] Not Dic.exists(Ray(n, ac)) [COLOR="Navy"]Then[/COLOR]
                [COLOR="Navy"]Set[/COLOR] Dic(Ray(n, ac)) = CreateObject("Scripting.Dictionary")
          [COLOR="Navy"]End[/COLOR] If
        [COLOR="Navy"]If[/COLOR] Not Dic(Ray(n, ac)).exists(Ray(n, ac + 1)) [COLOR="Navy"]Then[/COLOR]
                Dic(Ray(n, ac)).Add (Ray(n, ac + 1)), Nothing
        [COLOR="Navy"]End[/COLOR] If
    [COLOR="Navy"]Next[/COLOR] n
[COLOR="Navy"]Next[/COLOR] ac
   
c = 1
Cells(c, "F") = "Parent"
Cells(c, "G") = "Child"

[COLOR="Navy"]For[/COLOR] [COLOR="Navy"]Each[/COLOR] K [COLOR="Navy"]In[/COLOR] Dic.Keys
    [COLOR="Navy"]For[/COLOR] [COLOR="Navy"]Each[/COLOR] P [COLOR="Navy"]In[/COLOR] Dic(K)
        c = c + 1
        Cells(c, "F") = K
        Cells(c, "G") = P
    [COLOR="Navy"]Next[/COLOR] P
[COLOR="Navy"]Next[/COLOR] K
[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]Sub[/COLOR]
Regards Mick
 
Upvote 0
Thannks the code worked.
Now what if my columns are also varying. How to tackle that?
If you could please let me know that one.
 
Upvote 0
What if I now want to do the reverse. The source is in two column format and the output would be multiple levels.
 
Upvote 0
The original code should cover Multi columns of data.

Reversing your data is possible as below, but depending on its complexity there could be problems
Your data for reversing in "A & B" reversed results for previous data in columns "G to J".

Code:
[COLOR="Navy"]Sub[/COLOR] MG13Nov11
'[COLOR="Green"][B]Reverse[/B][/COLOR]
[COLOR="Navy"]Dim[/COLOR] Rng         [COLOR="Navy"]As[/COLOR] Range
[COLOR="Navy"]Dim[/COLOR] Dn          [COLOR="Navy"]As[/COLOR] Range
[COLOR="Navy"]Dim[/COLOR] c           [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Long[/COLOR]
[COLOR="Navy"]Dim[/COLOR] Dic         [COLOR="Navy"]As[/COLOR] Object
[COLOR="Navy"]Dim[/COLOR] R           [COLOR="Navy"]As[/COLOR] Range
[COLOR="Navy"]Dim[/COLOR] ac          [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Long[/COLOR]
[COLOR="Navy"]Dim[/COLOR] K           [COLOR="Navy"]As[/COLOR] Variant
[COLOR="Navy"]Dim[/COLOR] Txt         [COLOR="Navy"]As[/COLOR] Variant


[COLOR="Navy"]Set[/COLOR] Rng = Range(Range("B2"), Range("B" & Rows.Count).End(xlUp))
[COLOR="Navy"]Set[/COLOR] Dic = CreateObject("scripting.dictionary")
Dic.CompareMode = vbTextCompare

[COLOR="Navy"]For[/COLOR] [COLOR="Navy"]Each[/COLOR] Dn [COLOR="Navy"]In[/COLOR] Rng
   [COLOR="Navy"]If[/COLOR] Dn.Offset(, -1) <> "" [COLOR="Navy"]Then[/COLOR]
   [COLOR="Navy"]If[/COLOR] Not Dic.exists(Dn.Value) [COLOR="Navy"]Then[/COLOR]
         Dic.Add (Dn.Value), Dn.Offset(, -1)
    [COLOR="Navy"]Else[/COLOR]
        [COLOR="Navy"]Set[/COLOR] Dic(Dn.Value) = Union(Dic(Dn.Value), Dn.Offset(, -1))
    [COLOR="Navy"]End[/COLOR] If
[COLOR="Navy"]End[/COLOR] If
[COLOR="Navy"]Next[/COLOR] Dn


[COLOR="Navy"]Dim[/COLOR] n
c = 1
[COLOR="Navy"]For[/COLOR] n = Dic.Count To 1 [COLOR="Navy"]Step[/COLOR] -1
   ac = 10: c = c + 1
    Txt = Rng(n)
    [COLOR="Navy"]If[/COLOR] Application.CountIf(Rng.Offset(, -1), Txt) = 0 [COLOR="Navy"]Then[/COLOR]
    Cells(c, ac) = Txt
    Cells(1, ac) = "Level " & ac - 5
        [COLOR="Navy"]Do[/COLOR] [COLOR="Navy"]While[/COLOR] Dic.exists(Txt)
            ac = ac - 1
            Txt = Dic(Txt)
           Cells(1, ac) = "Level " & ac - 5
           Cells(c, ac) = Txt
        [COLOR="Navy"]Loop[/COLOR]
    [COLOR="Navy"]End[/COLOR] If
 [COLOR="Navy"]Next[/COLOR] n
[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]Sub[/COLOR]
Regards Mick
 
Upvote 0

Forum statistics

Threads
1,213,526
Messages
6,114,136
Members
448,551
Latest member
Sienna de Souza

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