简体   繁体   中英

Return array of objects from function - VBA

I need to "return" an Array of Objects from a function that I created using VBA. When I try to set the function as the array it gives me an error message, saying

Object is required.

I am not very used to VBA, and I can't fix this. Here is the function code:

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 is a "class" that I created. tempList is the array of objects from class ship that I need to return from the function sortedList .

The function works, it's just the return part that I can't make work.

Thanks for the help. If more information is necessary let me know!

Declare the function to return an array

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

and then assign the result without Set

sortedList = tempList

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM