迭代 VBA 字典?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1296225/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Iterate over VBA Dictionaries?
提问by mandroid
I'm using the Dictionary class in the MS Runtime Scripting library to store where labels are going to go for a report template. Is there a way to iterate over all the key value pairs in that dictionary like in Python?
我正在使用 MS 运行时脚本库中的 Dictionary 类来存储标签将用于报告模板的位置。有没有办法像在 Python 中一样迭代该字典中的所有键值对?
I just want to use the key as the row number (It's all going in column A) and the value will be the label header.
我只想使用键作为行号(都在 A 列中),值将是标签标题。
Something like:
就像是:
For Each key in dict
Range("A" & key).Value = dict(key)
Next key
回答by EBGreen
Try:
尝试:
For Each varKey In oDic.Keys()
Range("A" & varKey).Value = oDic(varKey)
Next
Note that the key iterator must be declared as a Variant.
请注意,密钥迭代器必须声明为Variant.

