Function for interchanging rows (1 Viewer)

AshikHusein

Registered User.
Local time
Today, 19:32
Joined
Feb 7, 2003
Messages
147
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

Registered User.
Local time
Today, 18:32
Joined
Mar 25, 2002
Messages
516
Howdy. Can you explain a little more on what you mean by "interchanging rows"?
________
Xv1700a
 
Last edited:

AshikHusein

Registered User.
Local time
Today, 19:32
Joined
Feb 7, 2003
Messages
147
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

Registered User.
Local time
Today, 18:32
Joined
Mar 25, 2002
Messages
516
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

Code:
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
________
Glass Bongs
 
Last edited:

Users who are viewing this thread

Top Bottom