![]() |
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 |
All times are GMT +1. The time now is 09:08 PM. |
Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com