Share via


How to reverse an array in Excel VBA

Question

Sunday, May 28, 2017 3:39 AM

I have the following code, which based on the logic it should work.

I want t to be (4,3,2,1), but at the end of the loop i get t=(4,3,3,4)

Sub try()
Dim t As Variant
t = Array(1, 2, 3, 4)
a = UBound(t)
For k = 0 To a
t(k) = t(a - k)
Next k
End Sub

Any ideas. Thanks.

All replies (2)

Sunday, May 28, 2017 4:57 AM âś…Answered

Try this:

For k = 0 To a \ 2
   Dim z
   z = t(k)
   t(k) = t(a - k)
   t(a - k) = z
Next k

Sunday, May 28, 2017 5:04 AM

Thanks a lot. It just  worked.