6

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!

1
  • Did you try to remove type specification of function output? Commented Aug 17, 2017 at 12:14

1 Answer 1

5

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
Sign up to request clarification or add additional context in comments.

2 Comments

Ok, it worked! Now, how to assign what I return from that function to a variable outside od the function? Like, Var1 = sortedList(param1, param2)
Exactly like that. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.