如何在VBA中检索字典的键值中的数组传递?

时间:2019-11-28 09:15:39

标签: excel vba dictionary

我填写了一个表格“ weekInfos”。我将其分配给字典“ dicoWeek”的键“ week”的值。 我不得不错过一些简单的东西,但是我不明白为什么调试部分什么都没显示。

能否请你帮我理解我的错误?

Sub test()
   Dim allCuid As Range, weeks As Range, week As Range
   Dim iCol as Long
   Dim weekInfos(6)

   Set dicoWeek = CreateObject("Scripting.Dictionary")

   For Each week In weeks
      For iCol = 0 To 5
         weekInfos(iCol) = .Cells(week.Row, iCol + 7).Value
      Next iCol
      weekInfos(iCol) = .Cells(week.Row, 15).Value
      dicoWeek.Add Key:=week.Text, Item:=weekInfos
      Debug.Print dicoWeek(Str(week.Text))
   Next week
End Sub

预先感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

这是我希望在您的代码中看到的更多内容-这是一个有效的示例。您应该将With ActiveSheet更新为您的工作Sheet的适当参考,并将Set weeks = Range("A1:A5")更改为实际范围

Sub test()
    Dim allCuid As Range, weeks As Range, week As Range
    Dim iCol As Long
    Dim dicoWeek As Object
    Dim weekInfos()

    Set dicoWeek = CreateObject("Scripting.Dictionary")

    With ActiveSheet
        Set weeks = Range("A1:A5")

        For Each week In weeks
            ReDim weekInfos(0 To 6)

            weekInfos = Application.Transpose(Application.Transpose(.Range(.Cells(week.Row, 7), .Cells(week.Row, 11)).Value2))
            ReDim Preserve weekInfos(LBound(weekInfos) To UBound(weekInfos) + 1)

            weekInfos(UBound(weekInfos)) = .Cells(week.Row, 15).Value
            dicoWeek.Add Key:=week.Value2, Item:=weekInfos

            Debug.Print Join(dicoWeek(week.Value2), ", ")
        Next week
    End With
End Sub

我还建议您删除例程中未使用的任何变量,例如allCuid