gpt4 book ai didi

excel - 使用后期绑定(bind)但不使用早期绑定(bind)时,字典出现运行时错误

转载 作者:行者123 更新时间:2023-12-02 09:48:39 24 4
gpt4 key购买 nike

我所做的是将字典放入子例程中的数组中

这就是定义

Dim Arr() As Variant
ReDim Arr(0 To Dict.Count - 1)
For c= 0 To Dict.Count - 1
Arr(c) = Dict.Keys(c) ' <~~~~~~ Error here
Next c

编译器说

Runtime Error 451: Property let procedure not defined and property get Procedure did not return an object.

它与

配合得很好
Public Sub SubRoutine(Dict As Scripting.Dictionary) 

但不与

Public Sub SubRoutine(Dict As Object) –

请引用Declare a dictionary without Microsoft Scripting Runtime

最佳答案

<Solution>

你这样做

Dim Arr() As Variant
ReDim Arr(0 To Dict.Count - 1)
For c = 0 To Dict.Count - 1
Arr(c) = Dict.Keys(c)
Next c

但是这样的循环是完全没有必要的。这就是Arr = Dict.Keys做。因此,只需执行以下操作即可代替上述内容

Dim Arr As Variant
Arr = Dict.Keys

额外的好处是这使得错误消失

</Solution>

<小时/>

但是为什么错误发生在代码的后期绑定(bind)版本而不是早期绑定(bind)版本?

<Educated guess>

通过早期绑定(bind),编译器知道 the .Keys method不带参数——它只返回一个数组。所以它解释Dict.Keys(c){returned array}(c)并返回c返回数组的第一个元素。

通过晚期绑定(bind),我猜对象容器不知道 .Keys方法不接受参数(也称为参数),因此它发送 c将其作为参数。但没有定义这样的 getter(或 setter),因此会出现错误。要补救,您可以apparently force it不向 Keys 发送任何参数方法是说 Dict.Keys()(c) ,这会带回 {returned array}(c)行为。

</Educated guess>

这是我第一次遇到这样的情况,其中后期绑定(bind)代码的行为与早期绑定(bind)不同。

关于excel - 使用后期绑定(bind)但不使用早期绑定(bind)时,字典出现运行时错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26585884/

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