Columns swap

sapka

Board Regular
Joined
Nov 9, 2009
Messages
110
Hi there,
I have this code to swap columns.
Code:
Sub test()
    Dim myrange As Range
 
    Set myrange = [A1:C5]
 
    myrange.Offset(, 10) = Application.Index(myrange, Evaluate("Row(" & myrange.Address & ")"), Array(3, 1, 2))
End Sub

It works well until I change myrange to something that starts not from the first row. Why is it happening this way? How to fix it?
Why if I change Evaluate part to myrange.Row it stops working correctly?

P.S. I am aware of other ways to swap columns, but this one for me would be the easiest.
 

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.
Try :-
Code:
Sub test()
 Dim myrange As Range
 Set myrange = [A2:C5]
 myrange.Offset(, 10) = Application.Index(myrange, Evaluate("Row( 1:" & myrange.Rows.Count & " )"), Array(3, 1, 2))
End Sub
Mick
 
Upvote 0
To answer your second question, you are evaluating the ROW() function. The parameter for the ROW() function can be either nothing... i.e. ROW() which returns the row number of the active row, or a range, for example ROW(A1), which returns the row number for the cell A1.

When you use myrange.row .... this will return the row number of the 1st cell in the range myrange. This value is an actual number and not a range. Hence, this will error out.

Hope this helps.
 
Upvote 0
I can use then .Rows function (myrange.Rows), which will return range rather than number, but it still does not work with this function.
 
Upvote 0
Have a play with this:-
Code:
[COLOR="Navy"]Sub[/COLOR] MG18Aug19
[COLOR="Navy"]Dim[/COLOR] ray, c, rng [COLOR="Navy"]As[/COLOR] Range
[COLOR="Navy"]Set[/COLOR] rng = Range("A2:c5")
'[COLOR="Green"][B]If you Evaluate the "Rng" to return an array of the range rows.[/B][/COLOR]
'[COLOR="Green"][B]This will return "2,3,4,5", But what you want is an array "1,2,3,4"[/B][/COLOR]
'[COLOR="Green"][B]To achieve this ,if you offset the range by the First row in[/B][/COLOR]
'[COLOR="Green"][B]"Rng" row(2)+1 as below you will return the require array "1,2,3,4"[/B][/COLOR]
MsgBox Join(Application.Transpose(Evaluate("row(" & rng.Address & ")")))
MsgBox Join(Application.Transpose(Evaluate("row(" & rng.Offset(-rng(1).Row + 1).Address & ")")))
'[COLOR="Green"][B]Even if you reset the range to "A1:A5" you still get the right answer[/B][/COLOR]
[COLOR="Navy"]Set[/COLOR] rng = Range("A1:c5")
MsgBox Join(Application.Transpose(Evaluate("row(" & rng.Offset(-rng(1).Row + 1).Address & ")")))
[COLOR="Navy"]Set[/COLOR] rng = Range("A2:c5")
'[COLOR="Green"][B]Because Rng(1) row = 2 and 2-1 = 1, and rng offset(-1) = "A1:A4" = rows "1,2,3,4)[/B][/COLOR]
MsgBox rng(1).Row
MsgBox rng.Offset(-rng(1).Row + 1).Address
MsgBox Join(Application.Transpose(Evaluate("row(" & rng.Offset(-rng(1).Row + 1).Address & ")")))
 rng.Offset(, 5) = Application.Index(rng, Evaluate("Row(" & rng.Offset(-rng(1).Row + 1).Address & " )"), Array(3, 1, 2))
'[COLOR="Green"][B]Which all equals :-[/B][/COLOR]
MsgBox Join(Application.Transpose(Evaluate("row(1:" & rng.Rows.Count & ") ")))
rng.Offset(, 5) = Application.Index(rng, Evaluate("Row(1:" & rng.Rows.Count & " )"), Array(3, 1, 2))
[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]Sub[/COLOR]
Regards Mick
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,602
Messages
6,179,841
Members
452,948
Latest member
UsmanAli786

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