检索词典VBA中的第n个键

是否有可能从字典中提取第n个键和其在VBA中的值? 就像是

For i = 1 To Counter + diccompare.Count - UBound(RecSource) WS.Cells(Lastrow + i, "A") = diccompare.Keys(UBound(RecSource) - Counter + i) Next 

我在哪里试图分配单元格(Lastrow + i)字典中的键值diccompare(UBound(RecSource) – Counter + i)

不知道我是否完全得到你,这样的事情

 Sub KeyTest() Dim d As New Scripting.Dictionary d.Add "Test1", 1 d.Add "Test2", 2 d.Add "Test3", 99 Debug.Print d.Keys()(1) End Sub 

你可以使用这个辅助函数:

 Function GetNthKey(dict As Dictionary, nth As Long) Dim arr As Variant With dict arr = .Keys GetNthKey = arr(nth - 1) End With End Function 

在你的“主要”代码中被利用如下:

 Dim diccompare As Dictionary Set diccompare = New Dictionary With diccompare .Add 1, "a" .Add 2, "b" .Add 3, "c" .Add 4, "d" End With MsgBox GetNthKey(diccompare, 2) '<--| returns "2", ie the 2nd key