PDA

View Full Version : Passing an item and its key to another collection



Anthony987
02-05-2017, 10:36 AM
In excel VBA Ihave a collection (colA) that has a first item of (It) with a key of ("Ke")
I want to pass this item AND its key to asecond collection (colB) using colA
as a reference
But.... using
colB.Add ColA(1) or colB.AddCol("Ke") only passes the item to the second collection and not itskey.
Is there any way I can pass the item and itskey to a different collection using colA as a reference.

SamT
02-05-2017, 11:50 AM
Try

colB.Add(colA("Ke"), "Ke")


also see the (Tools menu Reference to) "Microsoft Scripting Runtime" Dictionary Object

Paul_Hossler
02-05-2017, 06:43 PM
Not elegant, but a workaround would be to store the key as part of the item but you have to split it out each time



Option Explicit
Sub POC()
Dim ColA As Collection
Dim ColB As Collection

Dim Ke As String, It As String
Dim v As Variant


Set ColA = New Collection
Set ColB = New Collection

Ke = "ABC"
It = "DEF"


ColA.Add It & Chr(7) & Ke, Ke


v = Split(ColA(Ke), Chr(7))
MsgBox v(0)


v = Split(ColA("ABC"), Chr(7))
ColB.Add v(0), v(1)

MsgBox ColB("ABC")

End Sub