Chronicles.84
Chronicles.84

Reputation: 27

Flipping the order of a row

I'm trying to create An Array function that will reverse the order of a row of selected data.

Function Flip(R)

    Dim n as variant
    Dim i as long
    ReDim A(1, n)
    n = R.Rows.Columns.Count

    For i = 1 To n
        A(1, n - 1) = R.Cells(1, n)
    Next i
    Flip= A
End Function

The goal of this is to be able to reverse the order of a row of cells. For example: if the cells were ordered 20, 30, 40, 120, then using this array function should return the values 120, 40, 30, 20.

I know I'm having problems on how to call the n value to rearrange the array order. I've also tried making it "A(n, 1)" but this doesn't work either.

Upvotes: 0

Views: 48

Answers (1)

CLR
CLR

Reputation: 12279

You were using n before you'd assigned it a value. Also, the array pointers weren't quite right. This should do it:

Function Flip(R As Range)

Dim n As Variant
Dim i As Long

n = R.Cells.Count
ReDim A(1 To 1, 1 To n)

For i = 1 To n
    A(1, 1 + n - i) = R.Cells(1, i).Value
Next i

Flip = A

End Function

Upvotes: 2

Related Questions