Consulting

Results 1 to 10 of 10

Thread: Store rows in dictionary or collection

  1. #1

    Store rows in dictionary or collection

    Hello everyone
    I need to learn how to store a row or multiple rows in dictionary or collection but if possible with comments to get and understand the process
    Simple example I have IDs in column A and I need to get the unique ID and store each ID in sub-collection ...
    For example : the ID 8 is in three rows 4 - 5 - 8
    How to store those three rows in one sub-collection? and so on for the other IDs

    And how to put the results in worksheet .. (no matter the output) I just need how to populate the stored rows to the worksheet
    Attached Files Attached Files

  2. #2

  3. #3
    Thanks a lot for reply
    I have seen that link before and it is very useful but it seems I can't get it as a whole issue
    If possible to deal with the attachment and put comments .. I learn by example better
    Thanks advanced for help

  4. #4
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,726
    Location
    You can play around with this


    Option Explicit
    Sub Demo()
        Dim oDict As Object
        Dim rData As Range, rTemp As Range
        Dim iRow As Long
        Dim v As Variant
        Dim sKey As String
        
        'create late bound dictionary object
        Set oDict = CreateObject("Scripting.Dictionary")
        oDict.comparemode = vbTextCompare
        
        'the data to add
        Set rData = ActiveSheet.Cells(1, 1).CurrentRegion
        'skip header row
        For iRow = 2 To rData.Rows.Count
            
            'save the key value
            sKey = CStr(rData.Cells(iRow, 1).Value)
            
            'if the key is already in the dictionary, then Union the other row with the same key
            If oDict.exists(sKey) Then
                Set rTemp = oDict(sKey)
                Set rTemp = Union(rTemp, rData.Rows(iRow))
                Set oDict(sKey) = rTemp
            'otherwise just add the key and row
            Else
                oDict.Add sKey, rData.Rows(iRow)
            End If
        
        Next iRow
        
        'loop through the disctionary items
        For Each v In oDict.items
            MsgBox v.Address
        Next v
        Set oDict = Nothing
    
    End Sub
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  5. #5
    That's amazing Mr. Paul
    Thank you very much for great assistance
    Best Regards

  6. #6
    Sorry for disturbing you again Mr. Paul
    What if I need to store the values in each row (not to store the rows as ranges as you did)
    That's because I intend to deal with closed workbooks to gather data and I need to store the values for each key ...

    Thanks advanced for help

  7. #7
    Any help in that topic please?

  8. #8
    VBAX Expert Leith Ross's Avatar
    Joined
    Oct 2012
    Location
    San Francisco, California
    Posts
    552
    Location
    Hello Yasser,

    I have added this macro to the attached workbook. This uses the Dictionary and Variant Arrays to hold the Range data. When possible, it is easier and faster to manipulate arrays than objects.

    Here is the macro...
    Option Explicit
    
    
    Sub Macro1()
    
    
        Dim Cell    As Range
        Dim Data    As Variant
        Dim Dict    As Object
        Dim Item    As Variant
        Dim Key     As Variant
        Dim Rng     As Range
        Dim RngBeg  As Range
        Dim RngEnd  As Range
        Dim Wks     As Worksheet
        Dim x       As Long
        Dim y       As Long
        
            Set Wks = ThisWorkbook.Worksheets("Sheet1")
            
            Set RngBeg = Wks.Range("A2:D2")
            Set RngEnd = Wks.Cells(Rows.Count, RngBeg.Column).End(xlUp)
            
            If RngEnd.Row < RngBeg.Row Then Exit Sub
            
            Set Rng = Wks.Range(RngBeg, RngEnd)
            
            Set Dict = CreateObject("Scripting.Dictionary")
                Dict.CompareMode = vbTextCompare
                
                For Each Cell In Rng.Columns(1).Cells
                    Key = Trim(Cell)
                    Item = Cell.Resize(1, Rng.Columns.Count).Value
                    
                    If Not Dict.Exists(Key) Then
                        Dict.Add Key, Item
                    Else
                        ' To increase the rows in the 2-D array it must first be transposed.
                        ' Only the last dimension of an array can be resized.
                        Data = Application.Transpose(Dict(Key))
                            x = UBound(Data, 1)
                            y = UBound(Data, 2) + 1
                            ReDim Preserve Data(1 To x, 1 To y)
                        ' Transposing the array a second time restores the original order.
                        Data = Application.Transpose(Data)
                        
                        ' Load the new data.
                        For x = 1 To UBound(Item, 2)
                            Data(y, x) = Item(1, x)
                        Next x
                        
                        ' Save the Data.
                        Dict(Key) = Data
                    End If
                Next Cell
            
            Set Rng = Wks.Range("G2")
            
            For Each Item In Dict.Items
                x = UBound(Item, 1)
                y = UBound(Item, 2)
                Rng.Resize(x, y).Value = Item
                Set Rng = Rng.Offset(x, 0)
            Next Item
            
    End Sub
    Attached Files Attached Files
    Sincerely,
    Leith Ross

    "1N73LL1G3NC3 15 7H3 4B1L17Y 70 4D4P7 70 CH4NG3 - 573PH3N H4WK1NG"

  9. #9
    That's awesome Mr. Leith
    You are wonderful and I like your approach in coding
    Thank you very much for both of you for these great solutions

  10. #10
    VBAX Expert Leith Ross's Avatar
    Joined
    Oct 2012
    Location
    San Francisco, California
    Posts
    552
    Location
    Hello Yasser,

    You're welcome.
    Sincerely,
    Leith Ross

    "1N73LL1G3NC3 15 7H3 4B1L17Y 70 4D4P7 70 CH4NG3 - 573PH3N H4WK1NG"

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •