Option Explicit
Option Base 1
Sub shiftCircArray()
Dim iInputArray(3) As Integer
iInputArray(1) = 1
iInputArray(2) = 2
iInputArray(3) = 3
Dim iArray2() As Integer
iArray2 = RotateArrayRight(iInputArray)
End Sub
Function RotateArrayRight(ArrayToRotate)
Dim objNewArray() As Integer, iOldArrayPos As Integer, iNewArrayPos As Integer, iArrayLength
As Integer
Dim iPlacesToRotate As Integer
' Check that the array to be processed has been initialized
iPlacesToRotate = Application.CountA(ArrayToRotate)
If iPlacesToRotate <> 0 Then
' Check that the number of places to rotate is greater than zero - running the
' function with a value
' of places to rotate which is less than zero would cause problems, possibly causing
'the function to crash
If iPlacesToRotate > 0 Then
' get the length of the array to rotate, we'll be using it a few times so will
' load it into a local variable at the start of the function
iArrayLength = Application.CountA(ArrayToRotate)
' Array will be initialised from 0 to ArrayLength -1
' so it will contain ArrayLength elements
ReDim objNewArray(iArrayLength)
' This will remove any extra complete rotations through the array
' The mod operator returns the remainder of an integer divide operation
' Initialise the array position indexes
iOldArrayPos = iPlacesToRotate
iNewArrayPos = 1
' Copy objects from one array to the next
' First start at iPlacesToRotate into the old array
' and copy to the start of the new array.
While iOldArrayPos < iArrayLength + 1
objNewArray(iNewArrayPos) = ArrayToRotate(iOldArrayPos)
iOldArrayPos = iOldArrayPos + 1
iNewArrayPos = iNewArrayPos + 1
Wend
iOldArrayPos = 1
' Copy from the start of the old array into the end of the
' new array
While iOldArrayPos < iPlacesToRotate
objNewArray(iNewArrayPos) = ArrayToRotate(iOldArrayPos)
iOldArrayPos = iOldArrayPos + 1
iNewArrayPos = iNewArrayPos + 1
Wend
Else
MsgBox ("Values for 'Places to Rotate' must be greater than zero.")
End If
Else
MsgBox ("Cannot rotate an null array.")
End If
RotateArrayRight = objNewArray()
End Function
solved How to Shift an array circularly on VBA [closed]