【问题标题】:Returning Keys from nested dictionaries excel vba从嵌套字典excel vba返回键
【发布时间】:2017-12-30 18:58:12
【问题描述】:

我有嵌套字典的键返回到调用子函数的问题。它返回运行时错误 '424' Object Required

Set LoadCombinationDict = LCaseDict()
Set a = fs.CreateTextFile(file_name, True)

    For Each LCKey In LoadCombinationDict.Keys
        Line = LCKey
        For Each AddnlKey In AddnlCaseDict.Keys
            X = LoadCombinationDict(LCKey)(AddnlKey)
            Line = Line & ";" & X
        Next AddnlKey
        a.WriteLine (Line)
     Next LCKey
a.Close

下面是创建嵌套字典的函数

Function LCaseDict() as Scripting.Dictionary
'Some Code to create nested dictionary
End Function

如果我在子函数中包含上面的代码,字典就完美了。有没有办法将主字典和嵌套字典的键都返回给 excel vba 中的调用函数?

【问题讨论】:

  • 您正确地返回了字典吗?您的错误发生在哪一行?
  • 请显示您的 LCaseDict 和 LoadCombinationDict 代码。 x是什么?错误发生在哪一行?
  • 错误出现在 X = LoadCombinationDict(LCKey)(AddnlKey) 行中。感谢 Thomas 的建议,它运行良好。此外,我在每个 LCKey 循环之后重置了变量“Line”。

标签: excel vba dictionary nested


【解决方案1】:

这是遍历字典字典的方法

Set LoadCombinationDict = LCaseDict()
Set a = fs.CreateTextFile(file_name, True)

    For Each LCKey In LoadCombinationDict.Keys
        For Each AddnlKey In LoadCombinationDict(LCKey).Keys
            X = LoadCombinationDict(LCKey)(AddnlKey)
            Line = Line & ";" & X
        Next AddnlKey
        a.WriteLine (Line)
     Next LCKey
a.Close

【讨论】:

    猜你喜欢
    • 2022-01-13
    • 1970-01-01
    • 1970-01-01
    • 2018-06-27
    • 2020-08-06
    • 1970-01-01
    • 2020-08-27
    • 1970-01-01
    • 2021-12-16
    相关资源
    最近更新 更多