Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,290
Default How to assign a collection item?


The Dictionary object is better at this then a Collection,
however...

Sub ColTest()
Dim foo As Collection
Set foo = New Collection
foo.Add 10, "bar"
foo.Add 22, "bar1"
foo.Remove ("bar")
foo.Add 20, "bar", befo="bar1"

MsgBox "value: " & foo("bar")
Set foo = Nothing
End Sub
-----------
Jim Cone
San Francisco, USA
http://www.realezsites.com/bus/primitivesoftware




wrote in message
Maybe I'm just being dense, but I can't figure out how to do this.
Here's a code snippet, copied directly from a test function:
'----------------------------------------
Dim foo As New Collection
foo.Add 10, "bar"
MsgBox "value: " & foo("bar") ' Displays value: 10 in a message box
foo("bar") = 20 ' This gives a Run-time error '424': Object required
'----------------------------------------
If I try Set foo("bar") = 20, I get the same thing. If I try something
like this, I get the same thing, again, even if I try Set foo("bar") =i
'----------------------------------------
Dim foo As New Collection
Dim i as Integer: i = 10
foo.Add i, "bar"
MsgBox "value: " & foo("bar")
i = 20
foo("bar") = i ' This gives a Run-time error '424': Object required
'----------------------------------------
I also tried it without the key ("bar") and using a numeric index
instead with the same result. I also tried it using the
fully-qualified foo.Item("bar") and foo.Item(1) with the same result,
always a Run-time error '424'! So my question is, how do I assign an
element of a collection after it's been added? Do I really have to
remove it and re-add it, because that seems kind of silly. Or am I
just overlooking something really simple? (Definitely within the realm
of the possible...)

Or on the other hand, am I just using a Collection for something it's
not intended to be used for? I've got a dynamic set of variables that
I need to store information in and that change. Is there a better
structure to use or method of implementing something like this?
Thanks,
-KS

Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Reference to Item in Collection Chad Excel Programming 2 February 19th 06 02:37 PM
Cannot remove item from collection Kou Vang[_2_] Excel Programming 1 January 31st 06 08:24 PM
Retrieving a Collection Item Todd Huttenstine Excel Programming 2 October 27th 04 07:15 PM
Add Unique item to Collection Dick Kusleika[_3_] Excel Programming 8 January 21st 04 08:59 PM
Removing an Item From a Collection Dan Gesshel Excel Programming 4 November 1st 03 01:12 PM


All times are GMT +1. The time now is 04:07 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"