Copying and Pasting Non-Contiguous Ranges of Cells

Written by Allen Wyatt (last updated June 21, 2024)
This tip applies to Excel 2007, 2010, 2013, 2016, 2019, and Excel in Microsoft 365


6

Szilvia can create a selection set of cells, such that she has a non-contiguous range of cells selected. If she tries to copy those cells using Ctrl+C, Excel informs her that "this action will not work on multiple selections." Szilvia wonders if there a way to copy a non-contiguous range and then paste that range so that the cells are in the same relative position as in the original selection.

Apparently, this limitation of only allowing you to copy and paste contiguous ranges is the way that Excel is designed to work. The only way you can copy and paste a non-contiguous range is to use a macro—one that steps through each cell in a selection and pastes it relative to a target address. The following is an example:

Sub CopyPasteCells()
    Dim sTemp As String
    Dim sTarget As String
    Dim c As Range
    Dim pasteRng As Range

    sTemp = InputBox("Target cell?")
    sTarget = Trim(sTemp)
    If sTarget > "" Then
        Set pasteRng = ActiveSheet.Range(sTarget)
        For Each c In Selection
            c.Copy
            pasteRng.Range(c.Address).PasteSpecial xlPasteValues
        Next
    End If
    Application.CutCopyMode = False
End Sub

To use the macro, simply create your selection set of cells. Then, run the macro and specify the target cell for the pasting. The cells are pasted relative to that target cell on the current worksheet.

You should note that the macro does very little error checking. For instance, you could enter some totally bogus target cell address, and the macro would try to accommodate your request. (If the target address is too bogus, you'll get an error message.)

ExcelTips is your source for cost-effective Microsoft Excel training. This tip (5228) applies to Microsoft Excel 2007, 2010, 2013, 2016, 2019, and Excel in Microsoft 365.

Author Bio

Allen Wyatt

With more than 50 non-fiction books and numerous magazine articles to his credit, Allen Wyatt is an internationally recognized author. He is president of Sharon Parq Associates, a computer and publishing services company. ...

MORE FROM ALLEN

Keeping Table Rows Together

When you create a table that extends beyond a single page, you may want to make sure that the information in a table row ...

Discover More

Confirming File Conversions

Open a file that isn't a Word document and Word will still try to convert it to a Word document. If you want Word to let ...

Discover More

Turning Off Automatic Capitalization in Lists

By default, Word capitalizes letters that it thinks designate the beginning of a sentence. This includes at the beginning ...

Discover More

Excel Smarts for Beginners! Featuring the friendly and trusted For Dummies style, this popular guide shows beginners how to get up and running with Excel while also helping more experienced users get comfortable with the newest features. Check out Excel 2013 For Dummies today!

More ExcelTips (ribbon)

Sequentially Inputting Information

When entering data in a worksheet, you may only want to add information to the cells in a particular range. You can ...

Discover More

Three-Dimensional Transpositions

Excel makes it easy to transpose your data so that rows become columns and columns rows. It doesn't have a built-in ...

Discover More

Copying a Cell without Formatting

When you are copying a cell from one place to another (perhaps even to a different worksheet), you may not want to copy ...

Discover More
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

View most recent newsletter.

Comments

If you would like to add an image to your comment (not an avatar, but an image to help in making the point of your comment), include the characters [{fig}] (all 7 characters, in the sequence shown) in your comment text. You’ll be prompted to upload your image when you submit the comment. Maximum image size is 6Mpixels. Images larger than 600px wide or 1000px tall will be reduced. Up to three images may be included in a comment. All images are subject to review. Commenting privileges may be curtailed if inappropriate images are posted.

What is 8 + 7?

2022-07-11 09:40:05

J. Woolley

@Brian F
Change from c.Copy to c.Cut.


2022-07-10 22:46:12

Brian F

Thanks Allen, I figured out the position as A is the selected column and 1 is the row so to move one column over same row is B1. Is there a way to change the macro to cut and paste instead of copy?


2018-06-04 00:41:45

ETB

In my case I have a for loop which finds substrings and thereby column header names (with dynamic prefixes. )Non contiguous ranges are present within their underlying columns, and I have been able to copy and paste these ranges(with their relative positions), into existing charts. However, VBA will not paste "correctly".
Could I use the same logic as you show here to capture the relative positions ?


2018-01-28 06:39:28

Willy Vanhaelen

The macro in this tip does not what it is supposed to do. It only works if the first area of your selection starts in cell A1. In all other cases the destination is not the cell you enter in the input box but lower and /or to the right of it. How much depends of the offsite to cell A1.

Here is a macro that does the job as expected. Instead of copying cell by cell I make use of vba's Areas Collection and copy each area or contiguous block of the selection at a time which is of course faster.

Sub CopyMultiAreas()
Dim Area As Range, PasteRng As Range
Dim C As Integer, R As Double, X As Integer, Y As Double
On Error Resume Next
Set PasteRng = Application.InputBox("Target cell?", Type:=8)
If PasteRng Is Nothing Then Exit Sub
On Error GoTo 0
C = Selection.Column
R = Selection.Row
For Each Area In Selection.Areas
X = Area.Column
If X < C Then C = X
Y = Area.Row
If Y < R Then R = Y
Next Area
C = PasteRng.Column - C
R = PasteRng.Row - R
For Each Area In Selection.Areas
Area.Copy Cells(Area.Row + R, Area.Column + C)
Next Area
Application.CutCopyMode = False
End Sub


2018-01-27 10:33:33

tacenator

Is is possible to copy/paste non-contiguous ranges in limited circumstance: the ranges must come from the same rows for the data blocks. Example: select range a13 - d16; next, hold ctrl and select range f13 - g16 (standard dragging works in both instances). Hit Ctrl + C; go to another worksheet (or space in same worksheet) and paste using one of the paste functions (often I use "paste values"). The selected data will paste into adjacent columns in the new worksheet (maybe a[x] - f[y]). Works fine.


2018-01-27 07:25:42

Greg

That's pretty cool. Is there anyway to have the Target cell as an absolute position? The way it is now, i don't see how you can paste above or to the left of the first cell selected.


This Site

Got a version of Excel that uses the ribbon interface (Excel 2007 or later)? This site is for you! If you use an earlier version of Excel, visit our ExcelTips site focusing on the menu interface.

Newest Tips
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

(Your e-mail address is not shared with anyone, ever.)

View the most recent newsletter.