I have code that loops through a dictionary. the value for each key in the dictionary is a 2-item array (dictionary looks like name: [string, integer]). when I reference the dictionary later on, I can see and print the string and integer in the array belonging to the dictionary entry, but I can't change the integer through a normal assignment like dictionary(name)(2) = 5; after doing so in the code, I print the array value to a debug file, and the array value is the original value, not its changed value. I have no idea why this doesn't work, and how I can get it to work. everything I've read on arrays says you just assign array(0) = something.
here is part of the code below:
defining the original dictionary:
dim Dict as Object
Set Dict = CreateObject("Scripting.Dictionary")
for i = 1 to 10
strnum = "str"&i
Dict.Add strnum, array("str"&i+1,0)
next
'printing each item in dict returns "Str1: [str2,0]", etc. as it should
working with the dictionary:
For each Cell in Range("a1:a11")
If Cell.Value <> "" And Dict.exists(Cell.Value) Then
name = Cell.Value
range = Dict(name)(0)
If Dict(name)(1) = 1 Then
'we have already located the name here
Else
Dict(name)(1) = 1
s = "Setting the found flag to " & Dict(name)(1)
debug.print s
'Dict(name)(1) returns 0 when it should return 1
end if
end if
next cell
Range a1:a11 is Str1,Str2,Str3,Str4,Str5...Str11.
What can I do to fix this?
Set
beforeCreateObject
and usingAdd
in the loop like that will cause akey already associated with value
error). Please post code that actually works and reliably reproduces the problem. – John ColemanString
is not a valid VBA variable name – John Coleman