遍历VBA词典?


72

我正在使用MS Runtime Scripting库中的Dictionary类来存储要用于报告模板的标签。有没有办法像Python中那样遍历该字典中的所有键值对?

我只想将键用作行号(全部在A列中使用),并且该值将是标签标题。

就像是:

For Each key in dict
    Range("A" & key).Value = dict(key)
Next key

Answers:


129

尝试:

For Each varKey In oDic.Keys()
    Range("A" & varKey).Value = oDic(varKey)
Next

请注意,密钥迭代器必须声明为Variant


36
我要添加strKey应该声明As Variant,因为For Each不适用于Strings。
stenci 2013年
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.