Define parent/child relationship in two columns from data table

Marcus131975

New Member
Joined
Feb 9, 2018
Messages
14
Hello

Id be really grateful if anyone is able to tell me how to solve the following in VBA:

I need to extract (into two columns on a separate sheet ideally) the child and parent relationship from a table of data. The table data range varies between 5 and 10 columns, with the right most column being the child and the columns to the left showing the various parent up to the top parent. I am only concerned with the immediate parent of each value and any duplicates should be ignored.

Hopefully the below example might help explain this. Thank you.

Before


Book1
ABCD
1AAA1
2AAB2
3AAD3
4ABC4
Sheet1


Desired Result


Book1
AB
81A
92B
103D
114C
12BA
13DA
14CB
15A
Sheet1



Marcus
 

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
This seems to work with your sample data - see how it goes with your real data. It assumes your original data is in the active sheet when the code is run.

Code:
Sub ParentChild()
  Dim a As Variant
  Dim d As Object
  Dim i As Long, j As Long, uba As Long
  
  a = Range("A1").CurrentRegion.Value
  uba = UBound(a)
  Set d = CreateObject("Scripting.Dictionary")
  d.CompareMode = 1
  For j = UBound(a, 2) To 2 Step -1
    For i = 1 To uba
      If a(i, j) <> a(i, j - 1) Then
        d(a(i, j)) = a(i, j - 1)
      End If
    Next i
  Next j
  For i = 1 To uba
    d(a(i, 1)) = Empty
  Next i
  Sheets.Add After:=ActiveSheet
  Range("A1").Resize(d.Count, 2).Value = Application.Transpose(Array(d.Keys, d.Items))
End Sub
 
Last edited:
Upvote 0
Thank you very much Peter, I appreciate it.

I am using this for a custom loader but noticed when I compare it to the control file that it also requires the 'position' of the child, which is stored as a number.

That number starts at 31 from the bottom (child values) and decreased by 1 for each parent.

As from my data you can see that a value can be repeated within the hierarchy in multiple columns but, from what I can see, it uses the hierarchy value from the first column it is in from left to right, if it repeats.

Hopefully the example below will help (column numbering in first table for illustration only)
ABCD

<colgroup><col style="width: 25pxpx"><col><col><col><col></colgroup><thead>
</thead><tbody>
[TD="align: center"]1[/TD]
[TD="align: center"]28[/TD]
[TD="align: center"]29[/TD]
[TD="align: center"]30[/TD]
[TD="align: center"]31[/TD]

[TD="align: center"]2[/TD]
[TD="align: center"]A[/TD]
[TD="align: center"]A[/TD]
[TD="align: center"]A[/TD]
[TD="align: center"]1[/TD]

[TD="align: center"]3[/TD]
[TD="align: center"]A[/TD]
[TD="align: center"]A[/TD]
[TD="align: center"]B[/TD]
[TD="align: center"]2[/TD]

[TD="align: center"]4[/TD]
[TD="align: center"]A[/TD]
[TD="align: center"]A[/TD]
[TD="align: center"]D[/TD]
[TD="align: center"]3[/TD]

[TD="align: center"]5[/TD]
[TD="align: center"]A[/TD]
[TD="align: center"]B[/TD]
[TD="align: center"]C[/TD]
[TD="align: center"]4[/TD]

</tbody>
Sheet1



After

ABC

<colgroup><col style="width: 25pxpx"><col><col><col></colgroup><thead>
</thead><tbody>
[TD="align: center"]8[/TD]
[TD="align: center"]1[/TD]
[TD="align: center"]A[/TD]
[TD="align: center"]31[/TD]

[TD="align: center"]9[/TD]
[TD="align: center"]2[/TD]
[TD="align: center"]B[/TD]
[TD="align: center"]31[/TD]

[TD="align: center"]10[/TD]
[TD="align: center"]3[/TD]
[TD="align: center"]D[/TD]
[TD="align: center"]31[/TD]

[TD="align: center"]11[/TD]
[TD="align: center"]4[/TD]
[TD="align: center"]C[/TD]
[TD="align: center"]31[/TD]

[TD="align: center"]12[/TD]
[TD="align: center"]B[/TD]
[TD="align: center"]A[/TD]
[TD="align: center"]29[/TD]

[TD="align: center"]13[/TD]
[TD="align: center"]D[/TD]
[TD="align: center"]A[/TD]
[TD="align: center"]30[/TD]

[TD="align: center"]14[/TD]
[TD="align: center"]C[/TD]
[TD="align: center"]B[/TD]
[TD="align: center"]30[/TD]

[TD="align: center"]15[/TD]
[TD="align: center"]A[/TD]
[TD="align: center"][/TD]
[TD="align: center"]28[/TD]

</tbody>
Sheet1



.
If you have time, or inclination, to let me know how this could be achieved Id very grateful, but dont worry if not

Thanks again for your help
Marcus
 
Upvote 0
Again, this seems to work with that new sample data & expected result.
Code:
Sub ParentChild_v2()
  Dim a As Variant
  Dim d As Object
  Dim i As Long, j As Long, uba As Long
  
  a = Range("A1").CurrentRegion.Value
  uba = UBound(a)
  Set d = CreateObject("Scripting.Dictionary")
  d.CompareMode = 1
  For j = UBound(a, 2) To 2 Step -1
    For i = 2 To uba
      If a(i, j) <> a(i, j - 1) Then
        d(a(i, j)) = a(i, j - 1) & "," & a(1, j)
      End If
    Next i
  Next j
  For i = 2 To uba
    d(a(i, 1)) = "," & a(1, 1)
  Next i
  Sheets.Add After:=ActiveSheet
  Range("A1").Resize(d.Count, 2).Value = Application.Transpose(Array(d.Keys, d.Items))
  Columns("B").TextToColumns DataType:=xlDelimited, Tab:=False, Semicolon:=False, Comma:=True, Space:=False, Other:=False
End Sub
 
Upvote 0
Thank you so much Peter, that's perfect from what I can see. You have been a big help and it is much appreciated and makes my life over the next few days a lot easier.

Thanks again
Marcus
 
Upvote 0
Thank you so much Peter, that's perfect from what I can see. You have been a big help and it is much appreciated and makes my life over the next few days a lot easier.

Thanks again
Marcus
You are welcome. :)
 
Upvote 0

Forum statistics

Threads
1,223,909
Messages
6,175,310
Members
452,634
Latest member
cpostell

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