我在VBA中有一个带有键和值的集合对象,我想迭代它们并在调试控制台中显示它们。
这是我的代码:
' Generated error code array
Dim errCodeArr As New Scripting.Dictionary
Dim ItemKey As Variant
Set rsErrCodes = dbQuery("select errorcode from tbl_errorcodes order by errorcode")
On Error GoTo arrErr
While Not rsErrCodes.EOF
errCodeArr.Add rsErrCodes!errorCode, rsErrCodes!errorCode & "filler"
rsErrCodes.MoveNext
Wend
For Each ItemKey In errCodeArr
Debug.Print "Key: " & ItemKey
Debug.Print "Value: " & errCodeArr.Item(ItemKey)
Next
arrErr:
Debug.Print err.Description
错误说明: 此密钥已与此集合的元素
关联我已经改变了问题以反映这些变化,但现在我收到了一个关键错误。
答案 0 :(得分:2)
据我所知,集合无法实现,但可以使用词典:
Dim errCodeArr As New Scripting.Dictionary
Dim ItemKey As Variant
Set rsErrCodes = dbQuery("select errorcode from tbl_errorcodes order by errorcode")
On Error GoTo arrErr
While Not rsErrCodes.EOF
errCodeArr.Add CStr(rsErrCodes!errorCode), CStr(rsErrCodes!errorCode & "filler")
rsErrCodes.MoveNext
Wend
For Each ItemKey In errCodeArr
Debug.Print "Key: " & ItemKey
Debug.Print "Value: " & errCodeArr.Item(ItemKey)
Next
您可以在Excel here
中阅读有关usnig Dictionary的内容