VBA - Shift down values in an array with feeding from top

L

Legacy 143009

Guest
What would be the best practice to shift down values of an array while feeding from top at the same time?

VBA Code:
Dim myArr As Variant
myArr = Array("A", "B", "C")

For i = 0 To 1
  'What should be the code?
Next
'Expected result is myArr("C", "A", "B")
or
VBA Code:
For i = 0 To 2
  'What should be the code?
Next
'Expected result is myArr("B", "C", "A")
 

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!
An example:
VBA Code:
Function ArrShift(ByVal myArr As Variant) As Variant
    Dim TmpVar As Variant
    Dim I As Long
    
    For I = UBound(myArr) To LBound(myArr) Step -1
        Select Case I
        Case UBound(myArr)
            TmpVar = myArr(I)
            myArr(I) = myArr(I - 1)
        Case LBound(myArr)
            myArr(I) = TmpVar
        Case Else
            myArr(I) = myArr(I - 1)
        End Select
    Next I
    ArrShift = myArr
End Function


VBA Code:
Sub AShiftTest()
    Dim myArr As Variant, TmpVar As Variant
    Dim I As Long, S As String

    myArr = Array("A", "B", "C")

    S = " Start :" & Join(myArr, " ") & vbCr
    
    myArr = ArrShift(myArr)

    S = S & "Finish: " & Join(myArr, " ")
    MsgBox S
End Sub
 
Upvote 0
An example:
VBA Code:
Function ArrShift(ByVal myArr As Variant) As Variant
    Dim TmpVar As Variant
    Dim I As Long
 
    For I = UBound(myArr) To LBound(myArr) Step -1
        Select Case I
        Case UBound(myArr)
            TmpVar = myArr(I)
            myArr(I) = myArr(I - 1)
        Case LBound(myArr)
            myArr(I) = TmpVar
        Case Else
            myArr(I) = myArr(I - 1)
        End Select
    Next I
    ArrShift = myArr
End Function


VBA Code:
Sub AShiftTest()
    Dim myArr As Variant, TmpVar As Variant
    Dim I As Long, S As String

    myArr = Array("A", "B", "C")

    S = " Start :" & Join(myArr, " ") & vbCr
 
    myArr = ArrShift(myArr)

    S = S & "Finish: " & Join(myArr, " ")
    MsgBox S
End Sub
Thank you for the suggestion.
I am also thinkin about something like this. What is your opinion?
VBA Code:
Dim myArr(3) As String, tempArr(3) As String
Dim shift As Integer
myArr(0) = "A"
myArr(1) = "B"
myArr(2) = "C"

shift = 1
For i = 0 To Ubound(myArr) - 1
  tempArr((i + shift) Mod Ubound(myArr)) = tempArr(i)
Next
For i = 0 To Ubound(tempArr) - 1
  myArr(i) = tempArr(i)
Next
"shift" is the amount of offset you want to.
 
Upvote 0
In coding there are many ways to skin a cat so if your method works and does what you need, then ok . But I will point out that you loop through the array twice instead of just once.
 
Upvote 0

Forum statistics

Threads
1,215,453
Messages
6,124,930
Members
449,195
Latest member
Stevenciu

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top