Computing.Net > Forums > Office Software > swap 2 rows in excel

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

swap 2 rows in excel

Reply to Message Icon

Name: robsoft
Date: June 18, 2009 at 02:48:52 Pacific
OS: Windows Vista
Subcategory: Microsoft Office
Comment:

how do you swap 2 rows in excel 2007
i need a macro to pick 2 rows then swap then over



Sponsored Link
Ads by Google

Response Number 1
Name: DerbyDad03
Date: June 18, 2009 at 03:57:29 Pacific
Reply:

A quick answer is to record a macro as you do a "Cut - Insert Cut Cells" for the 2 rows that you want to swap.

Every time you run the macro, those 2 rows will swap.

Other than that, we're going to need some more detail.


0

Response Number 2
Name: DerbyDad03
Date: June 18, 2009 at 07:28:57 Pacific
Reply:

This will ask for the rows to swap.

Make sure you enter the lower row number first.

Sub RowSwapper()
rw1 = Application.InputBox("Enter Lower Row number")
rw2 = Application.InputBox("Enter Higher Row number")
    Rows(rw1).Cut
    Rows(rw2).Insert Shift:=xlDown
    Rows(rw2).Cut
    Rows(rw1).Insert Shift:=xlDown
End Sub

Macro writing tip:

Record what you want to do and then clean up/modify the code produced by the recorder.


0

Response Number 3
Name: robsoft
Date: June 18, 2009 at 08:30:44 Pacific
Reply:

hi DerbyDad03
tried your macro swaps rows then puts them back again
can you stop them from going back

thanks robsoft


0

Response Number 4
Name: Mike (by mmcconaghy)
Date: June 18, 2009 at 10:56:16 Pacific
Reply:

your macro swaps rows then puts them back again

Not exactly.
If the rows are adjacent then the macro works.
If the rows are not adjacent then the macro does not work.

MIKE

http://www.skeptic.com/


0

Response Number 5
Name: DerbyDad03
Date: June 18, 2009 at 12:06:41 Pacific
Reply:

I'm not sure what you guys are doing, but here's what happens when I use my code:

I start with this:

	
      A
1   One
2   Two
3   Three
4   Four
5   Five
6   Six

I run my code (copied directly from this forum) and enter 2 for the Lower Row and 5 for the Higher Row.

I end up with this:

       A
1   One
2   Five
3   Three
4   Four
5   Two
6   Six

Didn't I just swap rows 2 and 5?


0

Related Posts

See More



Response Number 6
Name: Mike (by mmcconaghy)
Date: June 18, 2009 at 12:29:01 Pacific
Reply:

I got it to work, but had to change the code to:

Sub RowSwapper()
rw1 = Application.InputBox("Enter Lower Row number")
rw2 = Application.InputBox("Enter Higher Row number")
Rows(rw2).Cut
Rows(rw1).Insert Shift:=xlDown
Rows(rw1).Cut
Rows(rw2).Insert Shift:=xlDown

End Sub


As I said in Response #4
If the rows are adjacent then the macro works.

What happened when you tried to swap non adjacent rows, is it would move the lower row up one but do nothing with the upper row.

EDIT:

I think we got Upper & Lower confused....
I was thinking Lower Row meant "lower down the sheet" not Lower Numbered Row. :-)

That's why mine worked for me the way I was working.


MIKE

http://www.skeptic.com/


0

Response Number 7
Name: DerbyDad03
Date: June 18, 2009 at 13:04:45 Pacific
Reply:

So, just to clarify for the OP:

With "Lower Row" meaning "Lower Numbered Row", then my original code works - without modification, correct?



0

Response Number 8
Name: Mike (by mmcconaghy)
Date: June 18, 2009 at 14:17:42 Pacific
Reply:

With "Lower Row" meaning "Lower Numbered Row", then my original code works - without modification, correct?

Correct.

MIKE

http://www.skeptic.com/


0

Response Number 9
Name: robsoft
Date: June 19, 2009 at 00:17:25 Pacific
Reply:

thanks for helping got it working ok now
it was something in my else in my sheet
messing it up working fine now

thank you


0

Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Office Software Forum Home


Sponsored links

Ads by Google


Results for: swap 2 rows in excel

increase no of rows in excel shet www.computing.net/answers/office/increase-no-of-rows-in-excel-shet/8272.html

Minimize Rows in Excel www.computing.net/answers/office/minimize-rows-in-excel/8830.html

Reverse rows in Excel 2003 www.computing.net/answers/office/reverse-rows-in-excel-2003/7798.html