Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
204 views
in Technique[技术] by (71.8m points)

GrandParent-Parent-Child logic in Excel /VBA

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

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

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...