从函数返回对象数组 - VBA

5
我需要从使用VBA创建的函数中“返回”一个对象数组。当我尝试将该函数设置为数组时,它会给出一个错误消息,称

需要对象。

我不太熟悉VBA,无法修复此问题。以下是函数代码:

Function sortedList(listRange As Integer, tempList() As ship) As ship
   Dim temp As ship
   Set temp = Nothing

   For i = listRange - 10 To 1 Step -1

       For j = 2 To listRange - 10
            If tempList(j - 1).Arrival > tempList(j).Arrival Then
                Set temp = tempList(j - 1)
                Set tempList(j - 1) = tempList(j)
                Set tempList(j) = temp
            End If
        Next j
    Next i

    'return tempList - how?
    Set sortedList = tempList

End Function

Ship 是我创建的一个“类”。tempList 是我需要从函数 sortedList 返回的来自 ship 类的对象数组。

这个函数是有效的,只是返回部分我无法使其工作。

谢谢帮助。如果需要更多信息,请告诉我!


你尝试过移除函数输出的类型规定吗? - AntiDrondert
1个回答

5

声明返回数组的函数

Function sortedList(listRange As Integer, tempList() As ship) As ship()

然后将结果分配给变量,无需使用Set关键字

sortedList = tempList

好的,它运行成功了!现在,如何将我从那个函数返回的内容赋值给函数外的变量呢?就像这样:Var1 = sortedList(param1, param2) - Pedro Destri
1
就是那样。 :) - Rory

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接