Swapping the contents of 2 ranges...

RobbieC

Active Member
Joined
Dec 14, 2016
Messages
376
Office Version
  1. 2010
Platform
  1. Windows
Hi there, I have 2 ranges - A1:A10 & B1:B10. I'm tring to work out how I can get the contents of 2 ranges to swap (regardless of whether they are blank or not) using a macro on a button

Any pointers would be really helpful.

Thanks
 
Would it make things easier if the ranges were named? ie A1:J1 named RowRange1 & A2:J2 named RowRange2
 
Upvote 0

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
Would naming the ranges be of any use? ie A1:J1 names RowRange1 and A2:J2 named RowRange2...

I'm clutching at straws now... (apologies for duplicate post - I didn't realise that it had gone over to page 2)
 
Last edited:
Upvote 0
Would it make things easier if the ranges were named? ie A1:J1 named RowRange1 & A2:J2 named RowRange2
Not necessary... give this code a try.
Code:
Sub SwapRanges()
  Dim Range1 As Variant, Range2 As Variant
  Range1 = Range("A1:J1")
  Range2 = Range("A2:J2")
  Range("A1:J1") = Range2
  Range("A2:J2") = Range1
End Sub
 
Upvote 0
Rick, you are a genius! Works like a trooper!

Thanks very much mate!
 
Upvote 0
Rick, you are a genius!
You are too kind... and wrong, but thanks for the sentiment.



Works like a trooper!

Thanks very much mate!
You are quite welcome, but it just occurred to me that we can use the standard Temp variable approach and eliminate one variable declaration and one line of code as well...
Code:
Sub SwapRanges()
  Dim Temp As Variant
  Temp = Range("A1:J1")
  Range("A1:J1") = Range("A2:J2").Value
  Range("A2:J2") = Temp
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,983
Messages
6,122,591
Members
449,089
Latest member
Motoracer88

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