将数组分配给属性let时的行为不一致

mar*_*vcd 6 arrays excel vba properties excel-vba

考虑以下代码:

Option Explicit

Public Property Let Dummy(v() As Integer)

End Property

Public Sub LetDummy(v() As Integer)

End Sub

Sub Foo()
    ReDim i(0 To 2) As Integer
    i(0) = 3
    i(1) = 45
    i(2) = 10

    Bar i ' internal error 51
End Sub

Sub Foo2()
    ReDim i(0 To 2) As Integer
    i(0) = 3
    i(1) = 45
    i(2) = 10

    Bar2 i ' no error
End Sub

Sub Foo3()
    ReDim i(0 To 2) As Integer
    i(0) = 3
    i(1) = 45
    i(2) = 10

    Dummy = i ' no error
End Sub

Sub Bar(j() As Integer)
    Dummy = j
End Sub

Sub Bar2(j() As Integer)
    LetDummy j
End Sub
Run Code Online (Sandbox Code Playgroud)

当我运行宏'Foo'时,我收到消息'内部错误51',但'Foo2'和'Foo3'运行正常.这种行为的原因是什么?或者它只是VBA中的一个错误?如何解决这个错误?

背景:在我的应用程序中,我想将作为函数参数提供的数组赋值给array类型的属性.

Jer*_*emy 4

对我来说,这看起来像是一个错误,因为错误 51 表示如果收到错误请联系 MS。

如果我创建一个局部变量,分配它,然后传递它,它似乎可以工作。

非常哈克。

Option Explicit

Private ary() As Integer

Public Property Get Dummy() As Integer()
    Dummy = ary
End Property

Public Property Let Dummy(v() As Integer)
    Debug.Print "In Dummy" & UBound(v)
    ary = v

End Property

Public Sub LetDummy(v() As Integer)
    Debug.Print "In LetDummy" & UBound(v)
End Sub

Sub Foo()
    ReDim i(0 To 2) As Integer
    i(0) = 3
    i(1) = 45
    i(2) = 10

    Call Bar(i)  ' internal error 51
End Sub

Sub Foo2()
    ReDim i(0 To 2) As Integer
    i(0) = 3
    i(1) = 45
    i(2) = 10

    Bar2 i ' no error
End Sub

Sub Foo3()
    ReDim i(0 To 2) As Integer
    i(0) = 3
    i(1) = 45
    i(2) = 10

    Dummy = i ' no error
End Sub

Sub Bar(j() As Integer)
    Dim i() As Integer
    i = j

    Dummy = i

    Dim x As Integer
    Dim myary() As Integer
    myary = Dummy
    For x = 0 To 2
        Debug.Print myary(x)
    Next
End Sub

Sub Bar2(j() As Integer)
    LetDummy j
End Sub
Run Code Online (Sandbox Code Playgroud)