gpt4 book ai didi

vba - 从 vba 中的函数返回字典。错误450

转载 作者:行者123 更新时间:2023-12-02 07:47:07 27 4
gpt4 key购买 nike

我编写了一个创建字典的函数,但遇到了奇怪的行为:我只能在函数内部通过键检索值。当我尝试使用函数返回的字典执行此操作时,我收到运行时错误“450”我的代码是:

Option Explicit
Function year_range_dict() As Object
Dim d
Set d = CreateObject("Scripting.Dictionary")
d.Add "a", "aaa"
d.Add "b", "bbb"
d.Add "c", "ccc"
If d.Exists("c") Then
MsgBox d("c")
End If
Set year_range_dict = d
End Function
Sub DefaultRates()
MsgBox year_range_dict()("a"), "outside of function"
End Sub

运行此代码时,我收到带有“CCC”的消息框,然后收到错误消息

最佳答案

我对这种白痴行为感到抱歉:我只忘记了一件事:设置。这是工作代码:

Option Explicit
Function year_range_dict() As Object
Dim d
Set d = CreateObject("Scripting.Dictionary")
d.Add "a", "aaa"
d.Add "b", "bbb"
d.Add "c", "ccc"
If d.Exists("c") Then
MsgBox d("c")
End If
Set year_range_dict = d
End Function
Sub DefaultRates()
Dim d
Set d=year_range_dict()
MsgBox d("a")
End Sub

关于vba - 从 vba 中的函数返回字典。错误450,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47458705/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com