Get our FREE VBA eBook of the 30 most useful Excel VBA macros.
Automate Excel so that you can save time and stop doing the jobs a trained monkey could do.
Email Address * First Name *
By entering your email address you agree to receive emails from Excel Off The Grid. We'll respect your privacy and you can unsubscribe at any time.

Swap ranges, rows and columns in Excel

VBA Code Snippets

Copying, pasting and moving cells are simple everyday tasks for an Excel user.  But what if something already occupies the area we want to paste to?  We have to move things into temporary locations before putting them into their final destination.  Wouldn’t it be better if there were a simple way to swap ranges, rows and columns?  Wouldn’t it be better if we could just select the ranges and click one button?  Yes, it would, and that is why I created the macro in this post.

Download the example file

I recommend you download the example file for this post.  Then you’ll be able to work along with examples and see the solution in action, plus the file will be useful for future reference.

Download Icon
Download the file: 0011 Swap ranges.zip

When to use the macro

Here is a scenario that I’m sure you can identify with.  You’ve received a workbook, which is to be used within a presentation.  However, you (or your manager) want to present the items in a different order.

Ranges swapped - example

Let the dragging, or copying and pasting begin.  Alternatively, if you have the following macro available, you can simply select the ranges and click.

VBA code

Here is the VBA code; paste it into a standard module and it will be available for use.  To make the code more reusable, your Personal Macro Workbook is the best place to save it.  Add a custom button to your ribbon, and it’s just one click away 🙂

Sub SwapSelectedRanges()

'Create variables to hold the ranges
Dim rng As Range
Dim tempRng As Variant
Dim areaCount As Long
Dim areaRows As Long
Dim areaCols As Long
Dim i As Integer
Dim j As Integer

Set rng = Selection
areaCount = rng.Areas.Count

'There must be at least two areas selected
If areaCount < 2 Then
    MsgBox "Please select atleast two ranges."
    Exit Sub
End If

'All areas must be the same shape
areaRows = rng.Areas(1).Rows.Count
areaCols = rng.Areas(1).Columns.Count
For i = 2 To areaCount
    If rng.Areas(i).Rows.Count <> areaRows Or _
        rng.Areas(i).Columns.Count <> areaCols Then
        MsgBox "All ranges must have the same number of rows and columns."    
        Exit Sub
    End If
Next i

'Check that ranges don't intersect with each other
For j = 1 To areaCount - 1
    For i = 1 + j To areaCount
        If Not Intersect(rng.Areas(i), rng.Areas(j)) Is Nothing Then
            MsgBox "Selected areas must not overlap."
        End If
    Next i
Next j

'Switch the ranges
tempRng = rng.Areas(areaCount).Cells.Formula
For i = areaCount To 2 Step -1
rng.Areas(i).Cells.Formula = rng.Areas(i - 1).Cells.Formula
Next i
rng.Areas(1).Cells.Formula = tempRng

End Sub
100 Excel VBA Macros

Do you know the fastest way to learn foreign languages?  It is to read, write, speak, and think in that language as often as possible.  Apart from speaking, programming languages are no different.  The more you immerse yourself in that language, the faster you will pick it up.

100 Excel Macros Book

Therefore, what most people like you need is lots of examples that you can practice.  That is why the 100 Excel VBA Macros eBook exists.  It’s the book for all Excel users who want to learn how to read and write Excel macros, save time, and stand out from their peers.  The book contains:

  • 100 example codes to practice reading and writing macros that will embed the language into your thinking.
  • An introduction to macros in Excel to ensure you can implement the VBA code in the book even if you have no prior knowledge.
  • Consistent code layout between examples to enable you to understand the structure and easily customize the code to meet your needs.
  • Downloadable workbook containing all the source code, so the examples can be added to your project to give you the benefit of VBA straight away.

Using the VBA code

The macro is simple to use:

  1. Select the first range
  2. Hold the Ctrl key, select the second range
  3. Run the macro

Done!  That’s it – how easy was that!

When using the macro, there are a few things to be aware of:

  • The individual ranges must be the same size (i.e., containing the same number of rows and columns).
  • The macro will include any hidden columns or rows
  • Cell references are maintained.  So, if a range contains a reference to cell A2, after clicking the macro, the range (which has now been moved) still references cell A2
  • Ranges cannot cross over with each other
  • Formatting does not move when the ranges are swapped
  • The code works on ranges which exist in the same worksheet
  • The macro will work with complete columns or rows, but will take longer than selecting a specific cell range.

Using with three or more ranges

Technically, the macro does not swap ranges, but rotates them.  When there are two ranges, rotating and swapping has the same result.

If we select more than two ranges, the macro will rotate them.  The first range moves to the second, the second moves to the third, etc.  Finally, the last range moves into the position initially occupied by the first range.

As before, select each individual range (holding the Ctrl key to select more ranges), then run the macro.



Get our FREE VBA eBook of the 30 most useful Excel VBA macros.
Automate Excel so that you can save time and stop doing the jobs a trained monkey could do.

Email Address * First Name *

By entering your email address you agree to receive emails from Excel Off The Grid. We’ll respect your privacy and you can unsubscribe at any time.


Don’t forget:

If you’ve found this post useful, or if you have a better approach, then please leave a comment below.

Do you need help adapting this to your needs?

I’m guessing the examples in this post didn’t exactly meet your situation.  We all use Excel differently, so it’s impossible to write a post that will meet everybody’s needs.  By taking the time to understand the techniques and principles in this post (and elsewhere on this site) you should be able to adapt it to your needs.

But, if you’re still struggling you should:

  1. Read other blogs, or watch YouTube videos on the same topic.  You will benefit much more by discovering your own solutions.
  2. Ask the ‘Excel Ninja’ in your office.  It’s amazing what things other people know.
  3. Ask a question in a forum like Mr Excel, or the Microsoft Answers Community.  Remember, the people on these forums are generally giving their time for free.  So take care to craft your question, make sure it’s clear and concise.  List all the things you’ve tried, and provide screenshots, code segments and example workbooks.
  4. Use Excel Rescue, who are my consultancy partner.   They help by providing solutions to smaller Excel problems.

What next?
Don’t go yet, there is plenty more to learn on Excel Off The Grid.  Check out the latest posts:

 

Leave a Reply

Your email address will not be published. Required fields are marked *