2013-11-03 69 views
3

我有一个词典,其中学校名称作为键和值的数组。一旦向字典添加了键值对,我希望能够在检查某些条件是否满足后更新数组的值。但是,即使满足条件,值也不会更新。有什么想法吗?在Excel VBA词典中更新值

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) 

回答

6

尝试这种情况:

创建第二阵列:

Dim vArray as variant 

redim vArray(0 to 2) 

然后字典阵列到新创建的数组指派为这样:

vArray = schdict.Item(schname) 

设置的值阵列:

vArray(1) = True 

最后指定数组的字典项:

schdict.Item(schname) = vArray