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

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
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
128293031
2AAA1
3AAB2
4AAD3
5ABC4

<colgroup><col style="width: 25pxpx"><col><col><col><col></colgroup><thead>
</thead><tbody>
</tbody>
Sheet1



After

ABC
81A31
92B31
103D31
114C31
12BA29
13DA30
14CB30
15A28

<colgroup><col style="width: 25pxpx"><col><col><col></colgroup><thead>
</thead><tbody>
</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,215,261
Messages
6,123,933
Members
449,134
Latest member
NickWBA

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