VBA Code for shifting blocks of data...

dominicwellsuk

New Member
Joined
Mar 23, 2011
Messages
28
I'm having some problems using VBA to shift rows of data within a list, up or down in a sequence. I have the code below, which works if I select a single row, however, if I select more than one row, the offset value no longer applies. Is there a way of varying the offset value, depending on how many rows I select, so all of the selected rows shift down?

There's some extra small things in the code which aren't important so I hope you can pick out the important bits.

Thanks. :)

---------------

Sub Row_Down()
Dim Response As VbMsgBoxResult
Response = MsgBox("Do You Want to Move Selected Line Item in Assy Sequence?", vbQuestion + vbYesNo)
If Response = vbNo Then Exit Sub
Selection.EntireRow.Cut
Selection.Offset(2, 0).Insert Shift:=xlDown
Selection.Offset(1, 0).Select
RowVariable = ActiveCell.Row
Range("B5").Select
ActiveCell.FormulaR1C1 = "1"
Range("B6").Select
ActiveCell.FormulaR1C1 = "2"
Range("B5:B6").Select
Selection.AutoFill Destination:=Range("B5:B104")
Rows(RowVariable & ":" & RowVariable).Select

Range("C5:H5").Select
Selection.AutoFill Destination:=Range("C5:H104"), Type:=xlFillFormats
Range("C5:H104").Select
End Sub
 

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
Try this

Code:
    With Selection
        .EntireRow.Cut
        '// Selection.Rows.Count returns the number of rows in selection
        .Offset(.Rows.Count + 1, 0).Insert Shift:=xlDown
        .Offset(0, 0).Select
    End With

in place of this
Code:
    Selection.EntireRow.Cut
    Selection.Offset(2, 0).Insert Shift:=xlDown
    Selection.Offset(1, 0).Select
 
Upvote 0

Forum statistics

Threads
1,224,581
Messages
6,179,668
Members
452,936
Latest member
anamikabhargaw

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