2017-08-17 203 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