Link to home
Start Free TrialLog in
Avatar of Saqib Husain
Saqib HusainFlag for Pakistan

asked on

Nested collections in VBA

I am trying to work on nested collections but am unsuccessful.

Can someone show me how to assign the given data to a collection such that each row is a collection and these collections are assigned to a parent collection.

I have attached a workbook with the data and also the code which I have which does not work as desired.

It has to be Collections. Not dictionary or any alternative.
Nested-collection.xlsm
Avatar of Wayne Taylor (webtubbs)
Wayne Taylor (webtubbs)
Flag of Australia image

I would create a new instance of 'drow' with each row iteration...

Sub collections()
    Dim data As New Collection
    Dim rw As Range
    Dim cel As Range    
    For Each rw In Range("A3:A8")        
        Dim drow As New Collection
        For Each cel In Range(rw, rw.End(xlToRight))
            drow.Add cel.Value
        Next cel
        data.Add drow        
    Next rw
End Sub

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Wayne Taylor (webtubbs)
Wayne Taylor (webtubbs)
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Saqib Husain

ASKER

Hi, when I run this code all items in data are the same from a to z
That works great.

Thanks

Saqib