Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

noob question about collections

Status
Not open for further replies.

Anastomosis

Programmer
May 11, 2009
4
US
Hi, I have just started with VBA, and I have a question that I cannot seem to answer using the documentation or other web pages.

Are Collections immutable or something? If I add an element using Add, then try to change that value, it gives me a "Run-time error '424' Object required" error.

Code:
Dim myColl As New Collection
myColl.Add 2, "Blue"

myColl("Blue") = 5

I have tried to use "Set myColl("Blue") = 5" and even "myColl(1) = 5". They all throw the same error.

If Collections are unchangeable, what's the point? Is there another container-like object which allows reference by keyword that allows you to actually change the values? (I would use an array, but I need to be able to access the data via keyword).
 


Hi,

Seems you must REMOVE, then ADD.

Check out the METHODS for a Collection.

Skip,
[glasses]Don't let the Diatribe...
talk you to death![tongue]

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 
You may also want to look at using a Dictionary object. You can change key values. You must have a Reference to Microsoft Scripting Runtime. An example:
Code:
'Create some variables
Dim a, d
Set d = CreateObject("Scripting.Dictionary")

d.Add 1, "Athens"     'Add some keys and items.
d.Add 2, "Belgrade"
d.Add 3, "Cairo"

MsgBox d(1)
' displays "Athens"

d.Item(1) = "York"

MsgBox d(1)   ' the same messagebox code as above
' displays "York"

Gerry
 
Thanks a lot, both Skip and fumei. I ended up using both after I checked on dictionaries (since I have experience in Python).

Peace!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top