【问题标题】:Excel VBA Dictionary of CollectionsExcel VBA 集合字典
【发布时间】:2016-10-20 00:25:08
【问题描述】:

我想要一个字典,其中键是字符串 providerName,值是 categoryId 的集合。但是,这是我得到的结果:

看起来key只是item1、item2、item3等,value只是providerName,而categoryIds的集合根本不存在。

我的代码:

For i = 2 To Selection.Rows.Count
providerName = SingleLine(i, 1)
categoryId = SingleLine(i, 3)
Dim categoryIdCollection As New Collection
If Not providerNamesDictionary.exists(providerName) Then
    categoryIdCollection.add (categoryId)
    providerNamesDictionary.add key:=providerName, Item:=categoryIdCollection
Else
    Dim tempCategoryIdCollection As Collection
    Set tempCategoryIdCollection = providerNamesDictionary(providerName)
    tempCategoryIdCollection.add (categoryId)
    Set providerNamesDictionary(providerName) = tempCategoryIdCollection
End If

如何将键作为 providerName,将值作为 categoryIds 的集合?

【问题讨论】:

    标签: vba excel


    【解决方案1】:

    这里有两个问题:

    1. 循环内的Dim 无法按预期工作。它在第一次遇到它时创建一个变量,然后它被忽略。净影响,您只需要创建一个集合并将所有类别 ID 添加到其中
      要修复第 1 项,请将 Dim categoryIdCollection As Collection 放在循环外,将 Set categoryIdCollection = New Collection 放在循环内(而不是 Dim ...
    2. 字典上的监视窗口显示的是键,而不是项目。要查看项目(即Collections),请观看providerNamesDictionary.items

    【讨论】:

      猜你喜欢
      • 2018-04-26
      • 1970-01-01
      • 2017-05-05
      • 2020-11-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多