VBA 字典嵌套字典 + 数组
功能展示为Excel VBA字典中嵌套字典,内层字典值为一个数组
Public Sub arrayInDictWithInDict()
myarr = Array("1", "2", "3")
Set mydic = CreateObject("Scripting.Dictionary")
keyTemp = "++abc"
str1 = "123"
str2 = "456"
Set dicTemp = CreateObject("Scripting.Dictionary")
Set mydic(keyTemp) = dicTemp '关键行,加Set为对象,需要新建一个字典赋给第1层的key,
mydic(keyTemp)(str1) = myarr
mydic(keyTemp)(str2) = myarr
If mydic("++abc").Exists("123") Then
Debug.Print "find 123" 'find 123
Else
Debug.Print "not find 123"
End If
If mydic("++abc").Exists("789") Then
Debug.Print "find 789"
Else
Debug.Print "not find 789" 'not find 789
End If
Debug.Print mydic("++abc")("456")(2) '3
temp = mydic("++abc")("456") '如果更新内层数组内的数据,需要先取出,更新后再存入才有效,不能直接索引更新
temp(2) = "4" '更新值
mydic("++abc")("456") = temp '重新赋值
Debug.Print mydic("++abc")("456")(2) '4
For Each oneKey In mydic.keys
Debug.Print oneKey '++abc
For Each oneT In mydic(oneKey).keys
Debug.Print oneT '123/456
Next
Next
End Sub
运行结果为:
find 123
not find 789
3
4
++abc
123
456