在VBA

时间:2018-05-09 05:49:49

标签: vba excel-vba excel

我正在尝试从函数中定义一个对象数组,但我总是得到编译错误Can't assign to array

这是我的代码:

Dim param1 As String
Dim param2 As String
Dim my_legs(1 To 4) As MyObject_Obj

Set my_legs = legBuilder(param1,param2)


private Function legbuilder (ByVal param1 As String,ByVal param2 As String)As MyObject_Obj

Dim my_legs(1 To 4) As MyObject_Obj

---Filling my Array---

legBuilder = my_legs

End Function

似乎我的函数是,通过设计不返回预期的类型(MyObject_Obj的数组)

我做错了吗?

2 个答案:

答案 0 :(得分:1)

Public Function ReturnArray(param1,param2) as variant
Dim A(4) as variant
A(0) = "One"
A(1) = "Two"
A(2)= "Three"
A(3) = "Four"
ReturnArray = A()
End Function

Sub test()
 MsgBox ReturnArray("A", "B")(2)

 End Sub

答案 1 :(得分:0)

忽略其余的,你无法做到

Set my_legs = legbuilder(param1, param2)

Set用于对象。您的“自定义”legbuilder实际上是返回一个数组,因此请删除Set