select row(s) and shift up one place automatically

  • Thread starter Thread starter bartman1980
  • Start date Start date
B

bartman1980

I want to swap the selected row with the one who is above. So I can
rearrange my products in the right way.

In this example I swap row 3 with 2.

Sub swaprows()
Rows("3:3").Select
Selection.Cut
Rows("2:2").Select
Selection.Insert Shift:=xlDown
End sub

But it is also possible that row 76 is swapped with row 75 depending
on which row is selected by the user.

And it is also possible that row 20 to 25 is selected and that it must
be 19 to 24.

Does anybody have an idea?
 
I want to swap the selected row with the one who is above. So I can
rearrange my products in the right way.

In this example I swap row 3 with 2.

Sub swaprows()
Rows("3:3").Select
Selection.Cut
Rows("2:2").Select
Selection.Insert Shift:=xlDown
End sub

But it is also possible that row 76 is swapped with row 75 depending
on which row is selected by the user.

And it is also possible that row 20 to 25 is selected and that it must
be 19 to 24.

Does anybody have an idea?

I think I already got it:

To shift up
Selection.Cut
Rows(ActiveCell.Row - 1 & ":" & ActiveCell.Row - 1).Select
Selection.Insert Shift:=xlDown

To shift down
Selection.Cut
Rows(ActiveCell.Row + 2 & ":" & ActiveCell.Row + 2).Select
Selection.Insert Shift:=xlDown
Rows(ActiveCell.Row - 1 & ":" & ActiveCell.Row - 1).Select
 
I think I already got it:

To shift up
Selection.Cut
Rows(ActiveCell.Row - 1 & ":" & ActiveCell.Row - 1).Select
Selection.Insert Shift:=xlDown

To shift down
Selection.Cut
Rows(ActiveCell.Row + 2 & ":" & ActiveCell.Row + 2).Select
Selection.Insert Shift:=xlDown
Rows(ActiveCell.Row - 1 & ":" & ActiveCell.Row - 1).Select

I couldn't get your shift down to work.

My attempts are...

Public Sub SwapRowsUp()
Selection.EntireRow.Select
Selection.Cut
Range(Cells(ActiveCell.Row - 1, 1), _
Cells(ActiveCell.Row + Selection.Rows.Count - 1, 1)).EntireRow.Insert
_
xlDown
End Sub

Public Sub SwapRowsDown()
Selection.EntireRow.Select
Selection.Cut
Range(Cells(ActiveCell.Row + 2 * Selection.Rows.Count, 1), _
Cells(ActiveCell.Row + 3 * Selection.Rows.Count - 1,
1)).EntireRow.Insert _
xlDown
End Sub

Ken Johnson
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top