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

Excel Facts

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)
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
 
Upvote 0
Hi Fluff, thanks alot for your help. The code worked.
 
Upvote 0
You're welcome & thanks for the feedback.
 
Upvote 0
Can anybody help me out with the following code. What's wrong in it?

Option Explicit
Option Base 1

Function ShiftVector(rng As Range, n As Integer)
Dim i As Integer, B(), nr As Integer
nr = rng.Rows.Count
ReDim B(nr, 1)
For i = 1 To nr - n
B(i, 1) = rng.Cells(i + n, 1)
Next i
For i = nr - n + 1 To nr
B(i, 1) = rng.Cells(i + n - nr, 1)
Next i
ShiftVector = WorksheetFunction.Transpose(B)
End Function
 
Upvote 0
What is wrong with the code in post#2?
 
Upvote 0
Hi Fluff, thanks for your reply. The above code in post 2 doesn’t work. I really dont know what is wrong in it.
 
Upvote 0
:unsure: then why did you say it worked in post#3?

In what way does it not work?
+Fluff New.xlsm
ABC
11heyhey
2hi33
3599
4hey77
53yoyo
6922
7711
8yohihi
9255
List
Cell Formulas
RangeFormula
B1:B9B1=ShiftVector(A1:A9,3)
C1:C9C1=ShiftVector(A1:A9,3)
Press CTRL+SHIFT+ENTER to enter array formulas.
Dynamic array formulas.
 
Upvote 0
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.
 
Upvote 0
Hi Fluff, the code you gave worked very well! But the code that i have posted in post 5 is not working. So wanted to know what is wrong in it.
 
Upvote 0

Forum statistics

Threads
1,215,046
Messages
6,122,852
Members
449,096
Latest member
Erald

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