vba 更新excel VBA字典中的值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19752379/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Updating values in excel VBA dictionary
提问by CodeNewbie
I have a Dictionary which has school names as keys and arrays for values. Once the addition of the key,value pairs to the dictionary is done, I want to be able to update the values of the array after checking if certain conditions are met. However, the values don't get updated even if the conditions are met. Any thoughts?
我有一个字典,它有学校名称作为键和数组的值。一旦将键值对添加到字典中,我希望能够在检查是否满足某些条件后更新数组的值。但是,即使满足条件,这些值也不会更新。有什么想法吗?
Dim nre(0 To 2) As Boolean
nre(0) = False
nre(1) = False
nre(2) = False
Dim schdict
Set schdict = CreateObject("Scripting.Dictionary")
For i = 2 To numrows
schname = ActiveSheet.Cells(i, schnamecolumn).Value
category = ActiveSheet.Cells(i, categorycolumn).Value
If schdict.Exists(schname) = False Then
schdict.Add schname, nre
End If
If category = "New Placement" Then
schdict.Item(schname)(0) = True
ElseIf category = "Renomination" Then
schdict.Item(schname)(1) = True
Else
schdict.Item(schname)(2) = True
End If
Next i
MsgBox schdict.Item("Division 01")(0)
回答by Trace
Try this:
尝试这个:
Create a second array:
创建第二个数组:
Dim vArray as variant
redim vArray(0 to 2)
Then assign the dictionary array to the newly created array as such:
然后将字典数组分配给新创建的数组,如下所示:
vArray = schdict.Item(schname)
Set the value of the array:
设置数组的值:
vArray(1) = True
And finally assign the array to the dictionary item:
最后将数组分配给字典项:
schdict.Item(schname) = vArray