Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I need to implement a Grandparent-Parent-Child kind of logic in Excel/VBA. But not sure what is the best approach?

Example

I get below details from an Excel addin, so I have to use Child output and parent in some cases.

Please note that at first instance, I would not have idea if a given ticker will have parent-child relationship. Only by digging further, I will get all details. In some case, I get Parent child, and other cases GrandParent - Parent -> Child relationship.

So kind of I am looking for recursive logic which drill down and pull all details.

1) GrandParent :- .ABC had two childerns, ( 1,2)

    .ABC -> .ABC1 , ABC2

2) Parent : ABC1 and ABC2 has 6 child each
            .ABC1 -> Child1, Child2, Child3,.. Child6
            .ABC2 -> Child1, Child2, Child3,.. Child6

3) Child : Child1, Child2, Child3,.. Child6 can have children too... 



       .ABC 
      /   
   .ABC1   .ABC2     
    \\   \\\ 
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
199 views
Welcome To Ask or Share your Answers For Others

1 Answer

Custom classes will do the job, it can have a collection to store child objects, which can then also have the same approach to have their own child objects.

Class clsObj

Private pChildObjects As New Collection
Private pName As String
Public Property Get ChildObjects() As Collection
Set ChildObjects = pChildObjects
End Property
Public Property Let ChildObjects(value As Collection)
Set pChildObjects = value
End Property
Public Property Get Name() As String
Name = pName
End Property
Public Property Let Name(value As String)
pName = value
End Property

Used in a sub:

Sub GenerateHierarchy()
Dim a As clsObj, b As clsObj, c As clsObj

Set a = New clsObj

a.Name = "Grandfather Bob"

Set b = New clsObj
    b.Name = "Parent A"
    a.ChildObjects.Add b, b.Name

Set c = New clsObj
    c.Name = "Child A"
    a.ChildObjects("Parent A").ChildObjects.Add c, c.Name

End Sub

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...