Find last column used on 2 rows and swap rows

Andy15

Board Regular
Joined
Apr 1, 2017
Messages
56
Hi all,

I have some imported data and rows 5 and 6 need swapping, the imported data can contain different amounts of data meaning that the number of columns used can be different.

I have tried the following but can't seem to get it to work

Code:
Sub Macro3()


Dim LastCol As Long


With ActiveSheet
        LastCol = .Cells(5, Columns.Count).End(xlToLeft).Column
        Range("A5:LastCol").Select
        Selection.Cut
        Range("A7:LastCol").Select
        Selection.Insert Shift:=xlDown


End With


End Sub

Thanks for any help
Andy
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
Does this do what you want...
Code:
Sub SwapRows5and6()
  Dim Data As Variant
  Data = Rows("5:6").Value
  Rows(5) = Application.Index(Data, 2, 0)
  Rows(6) = Application.Index(Data, 1, 0)
End Sub
 
Upvote 0
Should be able to do it with Cut/Insert as you were trying to do - just do the whole row instead of bothering about the last column
Code:
Sub Swap5and6()
  Rows(6).Cut
  Rows(5).Insert
End Sub
 
Upvote 0

Forum statistics

Threads
1,216,077
Messages
6,128,674
Members
449,463
Latest member
Jojomen56

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