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.
 
G

Guest

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top