Solved

Nested collections in VBA

Posted on 2015-02-11
4
139 Views
Last Modified: 2016-02-10
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
0
Comment
Question by:Saqib Husain, Syed
  • 2
  • 2
4 Comments
 
LVL 47

Expert Comment

by:Wayne Taylor (webtubbs)
ID: 40604412
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

0
 
LVL 47

Accepted Solution

by:
Wayne Taylor (webtubbs) earned 500 total points
ID: 40604415
Sorry, should have tested first. Try this instead....

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
        Set drow = Nothing
    Next rw
End Sub

Open in new window

0
 
LVL 43

Author Comment

by:Saqib Husain, Syed
ID: 40604422
Hi, when I run this code all items in data are the same from a to z
0
 
LVL 43

Author Closing Comment

by:Saqib Husain, Syed
ID: 40604430
That works great.

Thanks

Saqib
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A little background as to how I came to I design this code: Around 5 years ago I designed an add-in that formatted Excel files to a corporate standard, applying different cell colours and font type depending on whether the cells contained inputs,…
Convert between Excel file formats (.XLS, .XLSX, .XLSM) with/without macro option David Miller (dlmille) Intro Over this past Fall, I've had the opportunity to see several similar requests and have developed a couple related solutions associate…
This Micro Tutorial will demonstrate how to create pivot charts out of a data set. I also added a drop-down menu which allows to choose from different categories in the data set and the chart will automatically update.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question