vba collection - print keys and items

Mallesh23

Well-known Member
Joined
Feb 4, 2009
Messages
976
Office Version
  1. 2010
Platform
  1. Windows
Hi Team,

I am trying to print collections items and key, but unable to do it.
Can you please help me. Want to print collection using both for(i) and for(each) loops.

Data is in Column A and expected output is in Column H.


VBA Code:
Option Explicit
Sub Print_Collection_key_Item()

    Dim coll As New Collection
    Dim i As Long
    Dim row As Long

'====Read Data Column 2 and Column 4===
        For i = 2 To 8
            coll.Add Array(Cells(i, 2), Cells(i, 4)), Cells(i, 1)  'add column 2 and column 4 to collection
        Next i

    row = 2
           
'=========Write Data=========  This code works but unable to print key values
    For i = 1 To coll.Count
        'Cells(row, 8).Value = coll(i) ' Print Key
        Cells(row, 9).Value = coll(i)(0) 'Print Item Column 2
        Cells(row, 10).Value = coll(i)(1) 'Print Item Column 4
        row = row + 1
    Next i

''Print Collection using for each

'Range("H1:H8").Value = Application.Index(coll.items, 0, 0) 'Can we use single line code like this
'Range("H1:H8").Value = worksheetfunction.transpose(coll.item) 'like this not sure

        
        row = 2
    Dim itm As Variant
    '=======Write Data using Collection=====  how to print key and items using for each
        For Each itm In coll
            Cells(row, 8).Value = itm
            row = row + 1
        Next Key
End Sub


Book1
ABCDEFGHIJ
1NameNo of SixesNo of FoursTotal ScoresNameNo of SixesTotal Scores
2Sachin201090Sachin2090
3Dhoni1615100Dhoni16100
4Sehwag1814200Sehwag18200
5Virat138150Virat13150
6Yuvraj1820250Yuvraj18250
7Hardik3025300Hardik30300
8Bumrah2440Bumrah240
Sheet1


Thanks
mg
 
Last edited:

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
You'd have to store the keys as part of the item, or use something like a Dictionary instead.
 
Upvote 0

Forum statistics

Threads
1,214,587
Messages
6,120,405
Members
448,958
Latest member
Hat4Life

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top