迭代 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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 09:57:05  来源:igfitidea点击:

Iterate over VBA Dictionaries?

excelvba

提问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.