【问题标题】:Print the collection of a dictionary打印字典的集合
【发布时间】:2022-01-31 17:34:19
【问题描述】:

我创建了一个包含多个集合的字典,但我能找到的将项目的内容打印到列中的唯一方法是:

    j = 0
    For Each key In dict
     i = 1
     For Each values In dict(key)
        .Cells(1 + i, 1 + j).Value2 = values 
        i = i + 1
     Next
     j = j + 1
    Next

我可以使用dict.Items 之类的方法或更快的方法吗?

【问题讨论】:

  • 首先将每个集合转换为数组可能会更容易。只需一步即可更轻松地将数组写入工作表。
  • 多个集合(VBA 集合?)是什么意思。您是否知道 scripting.Dictionary 的 .Items 方法(如果那是 Dictory 是什么?)

标签: vba dictionary collections


【解决方案1】:

这个宏可以帮助你。

Sub FillDictionaryWithUniqueValuesAndWriteToSheet()
  Dim Vardat As Variant
  Dim objDict As Object
  Dim lngRow As Long

  Set objDict = CreateObject("Scripting.Dictionary")

  With Sheet1

  Vardat = Application.Transpose(.Range([a1], .Cells(Rows.Count, "A").End(xlUp)))

  For lngRow = 1 To UBound(Vardat, 1)
      objDict(Vardat(lngRow)) = 1
  Next lngRow

   .Range("H:H").ClearContents
   .Range("H1:H" & objDict.Count) = Application.Transpose(objDict.keys)

  End With

End Sub

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-11-07
    • 1970-01-01
    • 2022-08-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多