The function below reverses the order of the values in a one-dimensional array. Does nothing if the input parameter passed is not an array.

Function

VBA
Sub ReverseArray(arr As Variant) Dim i As Long, midPt As Long, lBd As Long, uBd As Long Dim vTemp As Variant If Not IsArray(arr) Then Exit Sub uBd = UBound(arr): lBd = LBound(arr) midPt = (uBd - lBd) \ 2 + lBd For i = LBound(arr) To midPt vTemp = arr(uBd) arr(uBd) = arr(i) arr(i) = vTemp uBd = uBd - 1 Next i End Sub

Parameters

NameOptional/
Required
Data typeDescription
arrRequiredVariantThe input array to be reversed.
Must be one-dimensional.

Usage example

VBA
Sub TestReverseArray() Dim i As Long Dim arr As Variant arr = Array(1, 2, 3, 4, 5) Call ReverseArray(arr) For i = LBound(arr) To UBound(arr) Debug.Print arr(i) Next i ' result: 5,4,3,2,1 End Sub