是否可以从字典中提取第n个密钥及其在VBA中的值?像
这样的东西For i = 1 To Counter + diccompare.Count - UBound(RecSource)
WS.Cells(Lastrow + i, "A") = diccompare.Keys(UBound(RecSource) - Counter + i)
Next
我试图在字典diccompare中分配Cell(Lastrow + i)键的值(UBound(RecSource) - Counter + i)
答案 0 :(得分:4)
不确定我是否完全找到了你,就像这样
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
答案 1 :(得分:2)
你可以使用这个助手功能:
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", i.e. the 2nd key