1

I have function, which return array:

Public Function BubbleSrt(ArrayIn, Ascending As Boolean)

Dim SrtTemp As Variant
Dim i As Long
Dim j As Long


If Ascending = True Then
    For i = LBound(ArrayIn) To UBound(ArrayIn)
         For j = i + 1 To UBound(ArrayIn)
             If ArrayIn(i) > ArrayIn(j) Then
                 SrtTemp = ArrayIn(j)
                 ArrayIn(j) = ArrayIn(i)
                 ArrayIn(i) = SrtTemp
             End If
         Next j
     Next i
Else
    For i = LBound(ArrayIn) To UBound(ArrayIn)
         For j = i + 1 To UBound(ArrayIn)
             If ArrayIn(i) < ArrayIn(j) Then
                 SrtTemp = ArrayIn(j)
                 ArrayIn(j) = ArrayIn(i)
                 ArrayIn(i) = SrtTemp
             End If
         Next j
     Next i
End If

BubbleSrt = ArrayIn

End Function

I trying to assign return array to another array, using this code:

sequence = BubbleSrt(unsorted, True)

Declaration:

Dim unsorted(2) As Integer
Dim sequence(2) As Integer

Compile error - Can't assign to array. The solutions is probably very easy, but i don't know, what to do with this.

1 Answer 1

1

Use this one instead:

Dim unsorted(2) As Integer
Dim sequence() As Integer

'initializing unsorted array

sequence = BubbleSrt(unsorted, True)

Btw, do you know that your function BubbleSrt modifies your unsorted array? E.g. if you initialize it with values {3,1,2} after calling BubbleSrt your unsorted would become "sorted": {1,2,3}. If you don't want it, add ByVal in your function declaration:

Public Function BubbleSrt(ByVal ArrayIn, Ascending As Boolean)
Sign up to request clarification or add additional context in comments.

2 Comments

Is there any possible to initialize sequence as global variable? Public sequence() as integer is not allowed.
Public sequence() As Integer works, but you should place it in the very top of the module: i.sstatic.net/qF7O2.png

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.