AshikHusein
12-06-2005, 10:35 AM
I would like to know if there is a way in VBA to interchange two rows (and its contents). Is there some ready function that can be used or would you have to do it by coppying, inserting new row, pasting contents to new row, and deleting the old row which was copied to the new row.
Thanking you.
Ashik
shades
12-06-2005, 10:37 AM
Howdy. Can you explain a little more on what you mean by "interchanging rows"?
AshikHusein
12-06-2005, 10:52 AM
Hi
For example if I have contents in row 1 and some other contents in row 2, the procedure should exchange the contents so row 1 will now have contents of row 2 and row 2 will have contents of row 1.
Thanks
Ashik
shades
12-06-2005, 11:09 AM
You can try something like this. It will need to be modified for your situation. Note that this cuts row 2 and puts it above Row 1. So technically it is not copy, but it is cutting and placing the even numbered row above the previous odd numbered row above it.
Always work on a backup copy
Sub XChange()
Dim i As Long
Dim LastRow As Long
LastRow = Cells(Cells.Rows.Count, 1).End(xlUp).Row
For i = 2 To LastRow Step 2
Rows(i & ":" & i).Cut
Rows(i - 1 & ":" & i - 1).Insert Shift:=xlDown
Next i
End Sub
AshikHusein
12-06-2005, 12:04 PM
That was very helpful. Thank you very much.