I have a tree defined in two columns, a parent and a child column.
For example, the table might be as follows:
Parent | Child |
---|---|
A | A1 |
A | A2 |
A1 | A1.1 |
A1 | A1.2 |
A1 | A1.3 |
A2 | A2.1 |
A1.2 | A1.2.1 |
A1.2 | A1.2.2 |
Given a node, I need all terminal children of that node. In the example above, given node A1, I need the list {A1.1, A1.2.1, A1.2.2, A1.3}.
I have tried the following code. I iterate through the parent column to find a match. If matched, I recursively find through the children nodes.
I think it’s close but it’s giving me the intermediary children nodes as well as the specified node itself. I can filter out the specified node easily enough (even post the VBA process, so I’m not worried about that). I’m struggling to think through removing the intermediary children nodes, however. Appreciate any help.
Public Function GetChildren(CheckString As String, CheckColumn As Range) As Variant
Dim temp As Variant
Dim Cell As Range
For Each Cell In CheckColumn
Set temp = Cell.Find(CheckString)
If (Not temp Is Nothing) Then
Call GetChildren(Cell.Offset(0, 1).Value2, CheckColumn)
End If
Next
Debug.Print (CheckString)
End Function
Sub Main()
Dim CheckColumn As Range
Dim CheckTable As String
Dim CheckString As String
CheckString = "E2270"
Set CheckColumn = ThisWorkbook.Sheets("Map").Range("CheckRange")
Call GetChildren(CheckString, CheckColumn)
End Sub