VBA code to shift elements of an array by n

Ria_Ko

New Member
Joined
Mar 18, 2020
Messages
43
Office Version
  1. 365
  2. 2019
  3. 2016
Platform
  1. Windows
Hello everyone. Hope all are doing well.
I needed help in writing a VBA code with arguments for a range (rng) and n that will shift the elements of an (m x 1) range up by n rows. The first n rows of the range rng will “wrap around” and appear at the bottom of the resulting vector. If anybody could help me out with the code using the elements that i have dimmed so that its easy for me to understand, it would be of great help.
I have attached an image for reference.

Option Explicit
Option Base 1

Function ShiftVector (rng As Range, n As Integer)
Dim nr As Integer, B() As Variant, i As Integer
nr = rng.Rows.Count
ReDim B(nr, 1)
B = rng
If i > (nr - n) Then
i = i + 1

ShiftVector = WorksheetFunction.Transpose(B)
End Function

I tried this much but couldn't move ahead of this.
Many thanks,
Ria
 

Attachments

  • Screenshot (9).png
    Screenshot (9).png
    131.1 KB · Views: 255
In what way is it not working?
 
Upvote 0

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.
FWIW, with Excel 365 with the SEQUENCE function, here is a non-looping one-liner that may do what you want?

VBA Code:
Function ShiftVector(rng As Range, n As Integer)
  ShiftVector = Application.Index(rng, Evaluate("mod(sequence(" & rng.Rows.Count & ",," & n & ")," & rng.Rows.Count & ")+1"), 1)
End Function

Enter the formula in the first cell only

Ria_Ko 2020-05-14 1.xlsm
AB
1ad
2be
3cf
4dg
5ea
6fb
7gc
Sheet1
Cell Formulas
RangeFormula
B1:B7B1=shiftvector(A1:A7,3)
Dynamic array formulas.
Hi Peter_SSs, i entered the formula in the first cell only still it does give me the required array elements . It gives me "hey" in all the cells.
 
Upvote 0
Change the last line of the code to
VBA Code:
ShiftVector = B
Because you have a 2D array you do not need to transpose it.
 
Upvote 0
Oh! Thanks alot i had been going crazy trying to figure out my mistake. Once again thank you so much Fluff! Means alot
 
Upvote 0
You're welcome & thanks for the feedback.
 
Upvote 0
How about
VBA Code:
Function ShiftVector(rng As Range, n As Integer)
Dim nr As Integer, B() As Variant, i As Integer
nr = rng.Rows.Count
ReDim B(1 To nr)
For i = 1 To nr
   If i <= n Then
      B(nr - n + i) = rng(i)
   Else
      B(i - n) = rng(i)
   End If
Next i
ShiftVector = WorksheetFunction.Transpose(B)
End Function
Hello! I have written the following code. The code is working but I am still not able to pass the assignment. I have been stuck on this for 1 week now. Can you tell me what might be the reason. I will be grateful.

Option Explicit
Option Base 1

Function ShiftVector(rng As Range, n As Integer) As Variant
Dim nr As Integer, i As Integer, B() As Variant
nr = rng.Rows.Count
ReDim B(nr) As Variant
For i = 1 To (nr - n)
B(i) = rng(i + n)
Next i
For i = nr - n + 1 To nr
B(i) = rng(i - nr + n)
Next i
ShiftVector = Application.WorksheetFunction.Transpose(B)

End Function
 
Upvote 0
Please start a new thread for this & give explain what you mean by "I am still not able to pass the assignment."
 
Upvote 0

Forum statistics

Threads
1,214,606
Messages
6,120,479
Members
448,967
Latest member
visheshkotha

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