vba - 访问子字典的值
问题描述
我似乎无法访问嵌套字典结构中内部字典的值...
最终,我想将 subdict 的值添加到数组中:
arrAll(i, intIndex) = dictWF(arrAll(i, 1)).Item(arrWFtypes(intIndexWFtype))
(外部和内部字典中的查找键是从数组中检索的,但它们不应该是问题,因为这些值被识别为 Debug.Print 告诉我)
以下是一些尝试找出我的错误,这也导致了其他错误:
简单的括号,没有方法/对象(错误 13:类型不匹配)
Debug.Print dictWF(arrAll(i, 1))(arrWFtypes(intIndexWFtype))
“.Item” x 2(错误 424:需要对象)(如果仅用于 subdict 也不起作用)
Debug.Print dictWF.Item(arrAll(i, 1)).Item(arrWFtypes(intIndexWFtype))
".Values" x 2(在某处找到)(错误 438:对象不支持此属性或方法)
Debug.Print dictWF.Values(arrAll(i, 1)).Values(arrWFtypes(intIndexWFtype))
如何检索 subdict 的值?我的错误很明显还是我应该提供更多代码?
谢谢您的回答!
编辑 02/17:
这是我填充字典的部分:
For i_row = 2 To UBound(arrDaten, 1)
If Not dictWF.Exists(arrDaten(i_row, intADRID) & arrDaten(i_row, intVGID)) Then
Set subdictWF = CreateObject("Scripting.Dictionary")
For i = LBound(arrWFtypes) To UBound(arrWFtypes)
subdictWF.Add Key:=arrWFtypes(i), Item:=0
Next i
subdictWF(arrDaten(i_row, intWFTYPE)) = subdictWF(arrDaten(i_row, intWFTYPE)) + 1
dictWF.Add Key:=arrDaten(i_row, intADRID) & arrDaten(i_row, intVGID), Item:=subdictWF
Else
dictWF(arrDaten(i_row, intADRID) & arrDaten(i_row, intVGID)).arrDaten(i_row, intWFTYPE) = dictWF(arrDaten(i_row, intADRID) & arrDaten(i_row, intVGID)).arrDaten(i_row, intWFTYPE) + 1
End If
Set subdictWF = Nothing
Next i_row
arrDaten 预先填充(打印示例并且有效)...
arrDaten = Application.Transpose(rngDaten)
...并且该范围的相关列作为索引移交给该函数,并在上面的代码中使用(intWFtype等)
解决方案
应该没有问题:
Dim dict1, dict2
Set dict1 = CreateObject("scripting.dictionary")
Set dict2 = CreateObject("scripting.dictionary")
dict2("innerKey") = "OK"
dict1.Add "outerKey", dict2
'these all work
Debug.Print dict1("outerKey")("innerKey")
Debug.Print dict1("outerKey").Item("innerKey")
Debug.Print dict1.Item("outerKey").Item("innerKey")
编辑:在这一行中,我猜你正在索引子字典而不是按键访问它
dictWF(k).arrDaten(i_row, intWFTYPE) = dictWF(k).arrDaten(i_row, intWFTYPE) + 1
也许尝试这样(添加几个变量以简化可读性:
Dim k, wfType
For i_row = 2 To UBound(arrDaten, 1)
k = arrDaten(i_row, intADRID) & arrDaten(i_row, intVGID)
wfType = arrDaten(i_row, intWFTYPE)
If Not dictWF.Exists(k) Then
Set subdictWF = CreateObject("Scripting.Dictionary")
For i = LBound(arrWFtypes) To UBound(arrWFtypes)
subdictWF.Add Key:=arrWFtypes(i), Item:=0
Next i
subdictWF(wfType) = subdictWF(wfType) + 1
dictWF.Add Key:=k, Item:=subdictWF
Else
dictWF(k)(wfType) = dictWF(k)(wfType) + 1
End If
Set subdictWF = Nothing
Next i_row
推荐阅读
- python - 水蟒。无法获得 ACU
- mysql - 如何计算平均间隔时间
- javascript - 从 Google Chrome 发送时跳过 Chromecast 选择
- java - c#匿名接口实现
- c# - 在 Linq 中,在 Where 子句之后的 Single() 有用吗?
- python-3.x - 从一个单词中获取所有可能的 pos 标签
- javascript - redux中isPlainObject函数的实现
- datatable - 使用jquery从mysql数据库中获取数据到数据表中
- python - signal.spectrogram 返回太多 hz
- java - 可以将 Lombok @AllArgsConstructor 注释与 Cucumber 一起使用吗?