Moving element contents of a 2 dimensional array, either up or down

Deutz

Board Regular
Joined
Nov 30, 2009
Messages
196
Office Version
  1. 365
Platform
  1. Windows
Hi and thanks in advance,

I am working with a 2 dimensional array in Excel that stores row numbers and names:

E.g.

Row numbersNames
1John
2Peter
3Fred
4Tony


I am looking for a way in VBA to move just one of the names, either up or down the array, leaving the row numbers as they are. (I do not need to resize the array):


So, if I wanted Fred to move up to the second element, and move Peter down one, this is how it should look:

Row numbersNames
1John
2Fred
3Peter
4Tony


Or if I wanted John to move down to the fourth element, and move Peter, Fred and Tony up one, then this is how it should look:

Row numbersNames
1Peter
2Fred
3Tony
4John


Thanks kindly
 

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
How about:

ABCDE
1INOUT
21John1Peter
32Peter2Fred
43Fred3Tony
54Tony4John
Sheet1


VBA Code:
Sub Test()

    Dim MyArray As Variant
    
    MyArray = Range("A2:B5").Value
    
    Call MoveElement(MyArray, 2, 1, 4)  'John to 4th
    Range("D2:E5").Value = MyArray
    
End Sub
Sub MoveElement(a As Variant, col As Long, From As Long, Too As Long)

    Dim temp As Variant
    Dim i As Long, s As Long
    
    s = IIf(From > Too, -1, 1)
    temp = a(From, col)
    
    For i = From To Too - s Step s
        a(i, col) = a(i + s, col)
    Next i
    a(Too, col) = temp

End Sub
 
Upvote 1
Solution
Thanks Stephen for your prompt solution that works as required. Much appreciated.

I see you assign the range to the array directly, withouth looping ... MyArray = Range("A2:B5").Value

I have only the names in the actual project worksheet column and need to add the row numbers as well to the array. Just wondering if there is an easy way to perhaps loop through the rows and add the row numbers along with the names to the 2 dim array?
 
Upvote 0
There will be a few ways you could do this. Here's one:

ABCD
1INOUT
2John1John
3Peter2Peter
4Fred3Fred
5Tony4Tony
Sheet1

VBA Code:
Sub Test()

    Dim MyArray As Variant

    With Range("A2:A5")
        MyArray = Evaluate("=HSTACK(SEQUENCE(" & .Rows.Count & ")," & .Address & ")")
        .Offset(, 2).Resize(.Rows.Count, 2).Value = MyArray     'Just to show MyArray
    End With

End Sub

But why store the row numbers? If you're looking at MyArray(i,j), you know the row number is i without having to also store i in the array.
 
Upvote 0
There will be a few ways you could do this. Here's one:

ABCD
1INOUT
2John1John
3Peter2Peter
4Fred3Fred
5Tony4Tony
Sheet1

VBA Code:
Sub Test()

    Dim MyArray As Variant

    With Range("A2:A5")
        MyArray = Evaluate("=HSTACK(SEQUENCE(" & .Rows.Count & ")," & .Address & ")")
        .Offset(, 2).Resize(.Rows.Count, 2).Value = MyArray     'Just to show MyArray
    End With

End Sub

But why store the row numbers? If you're looking at MyArray(i,j), you know the row number is i without having to also store i in the array.
Thanks again Stephen. That is a very neat way to do it.

Yes, you are right, I did not think of that about the row numbers.

Kind regards
Deutz
 
Upvote 0

Forum statistics

Threads
1,223,909
Messages
6,175,313
Members
452,634
Latest member
cpostell

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