你好友善的人。这个想法如下 - 我有一个字典词典,我想用循环设置它而不是每个字典。知道怎么做吗?
到目前为止,我有这样的事情:
Public Sub mains()
Dim my_dict As Object
Dim d1 As Object
Dim d2 As Object
Dim d3 As Object
Dim d4 As Object
Dim d5 As Object
Dim d6 As Object
Dim d7 As Object
Dim d8 As Object
Dim i As Long
Set my_dict = CreateObject("Scripting.dictionary")
For i = 1 To 8
set cstr(d & i) = createobject("scripting.dictionary")
Next i
End Sub
VBA可行吗? set cstr(“d”& i)当然会给出错误,但是我想要类似的东西而不是逐个设置字典。
答案 0 :(得分:2)
您无法以编程方式构造变量名称。改为使用数组。
Dim dicts(8) As Variant
For i = 0 To 7
Set dicts(i) = New Dictionary
Next i
或者,由于您的目标是构建词典字典,因此您可以直接执行此操作:
Dim dict as new Dictionary, tmp as Dictionary
For i = 0 to 7
Set tmp = new Dictionary
dict.add "d" & i + 1, tmp
next i