5/08/2015

Simple Excel VBA macros for swapping two rows

Sub SwapRanges()
  Dim ra As Range: Set ra = Selection
  msg1 = "You have to select TWO ranges of cells of the same size"
  msg2 = "You have to select 2 ranges of cells of the SAME size"
  If ra.Areas.Count <> 2 Then MsgBox msg1, vbCritical,"Error":Exit Sub
  If ra.Areas(1).Count <> ra.Areas(2).Count Then MsgBox msg2, vbCritical,"Error":Exit Sub
  Application.ScreenUpdating = False
  arr2 = ra.Areas(2).Value
  ra.Areas(2).Value = ra.Areas(1).Value
  ra.Areas(1).Value = arr2
End Sub
p.s.
And not only rows. You can swap any 2 equal areas:

How to use VBA macros:

No comments:

Post a Comment