2017-08-17 202 views
4

我需要从使用VBA创建的函数“返回”对象数组。当我尝试设置的功能它给了我一个错误信息的阵列,说从函数返回对象数组 - 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是来自类ship的对象数组,我需要从函数sortedList返回。

该功能起作用,它只是我无法工作的返回部分。

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

+0

你尝试删除功能输出的类型规范的结果呢? – AntiDrondert

回答

4

声明函数返回一个数组

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

,然后分配不Set

sortedList = tempList 
+0

好吧,它的工作!现在,如何将我从该函数返回的内容分配给该函数之外的变量?像,Var1 = sortedList(param1,param2) –

+1

正是这样。 :) – Rory