Collection in vba

P

Pierre

Hello,

In a vb collection, I would like to retrieve the key of each items.
Example :

dim col as Collection
col("k1")="a"
col("k2")="b"
.....
for each k in col
'here k equals "a", "b", ......
'and I would like to get "k1", "k2", ....
next

The "for each" structure gives the item itself and not its ket (in vbscript
we get the key).

How to do to get the key, or to retrieve the key from the item ?

Thanks.
Regards.

Pierre.
 
S

Sergey Poberezovskiy

IN VBA collectiion you cannot enumerate the keys.
But should you use Scripting.Dictionary instead - you can utilize Keys
property for that purpose.

HTH
 
A

Alex Dybenko

Hi Pierre,
you can store your own objects - then you can get keys.
create a new class module, say MyClass
make 2 public variables:

public Key as string
public Val as string

then add this objects to collection:

dim o as MyClass
set o=new MyClass
o.Key="k1"
o.Val="a"
col.add o, "k1"

....
then
for each o in col
debug.pring o.key, o.val
next o
 
Top