Please Note: This article is written for users of the following Microsoft Excel versions: 2007, 2010, 2013, and 2016. If you are using an earlier version (Excel 2003 or earlier), this tip may not work for you. For a version of this tip written specifically for earlier versions of Excel, click here: Transferring Data between Worksheets Using a Macro.

Transferring Data between Worksheets Using a Macro

by Allen Wyatt
(last updated September 17, 2016)

11

Leonard is writing a macro to transfer data from one worksheet to another. Both worksheets are in the same workbook. The data he wants to transfer is on the first worksheet and uses a named range: "SourceData". It consists of a single row of data. Leonard wants to, within the macro, transfer this data from the first worksheet to the first empty row on the second worksheet, but he's not quite sure how to go about this.

There are actually several ways you can do it, but all of the methods have two prerequisites: The identification of the source range and the identification of the target range. The source range is easy because it is named. You can specify the source range in your macro in this manner:

Set rngSource = Worksheets("Sheet1").Range("SourceData")

Figuring out the first empty row in the target worksheet is a bit trickier. Here's a relatively easy way to do it:

iRow = Worksheets("Sheet2").Cells(Rows.Count,1).End(xlUp).Row + 1
Set rngTarget = Worksheets("Sheet2").Range("A" & iRow)

When completed, the rngTarget variable points toward the range of cell A in whatever the first empty row is. (In this case, an empty row is defined as any row that doesn't have something in column A.)

Now all you need to do is put these source and target ranges to use with the Copy method:

Sub CopySource()
    Dim rngSource As Range
    Dim rngTarget As Range
    Dim iRow As Integer

    Set rngSource = Worksheets("Sheet1").Range("SourceData")
    iRow = Worksheets("Sheet2").Cells(Rows.Count,1).End(xlUp).Row + 1
    Set rngTarget = Worksheets("Sheet2").Range("A" & iRow)
    rngSource.Copy Destination:=rngTarget
End Sub

Note that with the ranges defined, all you need to do is use the Copy method on the source range and specify the target range as the destination for the operation. When completed, the original data is still in the source range, but has been copied to the target.

ExcelTips is your source for cost-effective Microsoft Excel training. This tip (6131) applies to Microsoft Excel 2007, 2010, 2013, and 2016. You can find a version of this tip for the older menu interface of Excel here: Transferring Data between Worksheets Using a Macro.

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

Making AutoComplete Work for an Entire Column

AutoComplete is a great feature for quickly adding data to a worksheet. If you are confused by why some things are picked up ...

Discover More

Searching Through Many Workbooks

If you have a folder that contains dozens or hundreds of workbooks, you may need to search through those workbooks to find ...

Discover More

Including Footnotes and Endnotes in Word Counts

When you have Word calculate how many words there are in a document, it normally doesn't pay attention to text in footnotes ...

Discover More

Program Successfully in Excel! John Walkenbach's name is synonymous with excellence in deciphering complex technical topics. With this comprehensive guide, "Mr. Spreadsheet" shows how to maximize your Excel experience using professional spreadsheet application development tips from his own personal bookshelf. Check out Excel 2013 Power Programming with VBA today!

More ExcelTips (ribbon)

Swapping Two Strings

Strings are used quite frequently in macros. You may want to swap the contents of two string variables, and you can do so by ...

Discover More

Running Macros on Hidden Worksheets

Excel allows you to hide worksheets so that they aren't visible to those using your workbook. Hiding worksheets has a ...

Discover More

Deriving an Absolute Value in a Macro

Need to figure out an absolute value within your macro code? It's easy to do using the Abs function, described in this tip.

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}] 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 3 + 8?

2017-05-11 10:52:13

Alan Elston

Edit: The start of one of my posts should have read “target” not “source”
“….. better just add, that when pasting or pastespecialling , the target……..”


2017-05-11 06:50:07

Alan Elston

@ zeddy,
As an Excel and VBA ( and computer ) novice , I certainly agree it can be very helpful to be precise when naming things. It is so easy to get lost and confused .. Certainly it sounds at first thought that Copy might be more appropriate for what Allen or I was doing here.
I have noticed the word transfer used a lot in computing, for the actual “transport” of data from one place to another, - Often in such a case you end up with data at the target and still at the source.
I am finding imprecise use in computing of words very frustrating… Getting clarity can really be like biting on a stone sometimes..
Alan


2017-05-11 06:27:33

Alan Elston

@ zeddy,
As an Excel and VBA ( and computer ) novice , I certainly agree it can be very helpful to be precise when naming things. It is so easy to get lost and confused .. Certainly it sounds at first thought that Copy might be more appropriate for what Allen or I was doing here.
I have noticed the word transfer used a lot in computing, for the actual “transport” of data from one place to another, - Often in such a case you end up with data at the target and still at the source.
I am finding imprecise use in computing of words very frustrating… Getting clarity can really be like biting on a stone sometimes..
Alan


2017-05-11 06:18:04

Alan Elston

Oops….
I had better just add, that when pasting or pastespecialling , the source range can be the entire range you want filled or can be just the top left. ( Excel guesses that you want the entire Range pasted out )

But for the values way of doing it I showed, the source range needs to be the entire range you want filled.
Depending on exactly what you are doing, that might mean that you would need to apply the Resize property to your target range so that it matches the size of the source range.

Something like this

rngTarget .Resize(rngSource.Rows.Count, rngSource.Columns.Count).Value = rngSource.Value

(Things in VBA and Excel tend often to be referenced to, or default to, top left)


2017-05-11 05:49:31

Alan Elston

Hi David
Your welcome, thanks fort he feedback.
Usually VBA can replicate the various options you have in Excel.
Check this out:
https://msdn.microsoft.com/de-de/library/office/ff839476.aspx
then from that - https://msdn.microsoft.com/de-de/library/office/ff837425.aspx
Or Run a macro recording when you copy and then paste using the different options:
PatseSpecial.JPG: http://imgur.com/FWA0Ar3

_.....

Note , if you are only interested in copying values, ( whether that is formula or “value” values ), then you can refer to those using the various Range object values Properties

So for example , in Allen’s or your codes, replace a code line like this:

rngSource.Copy Destination:=rngTarget

with one of these and see what happens:

rngTarget.Value = rngSource.Formula
rngTarget.Value = rngSource.Value

The last line should do what you originally wanted

Alan


2017-05-10 16:12:01

David

By the way, that worked.


2017-05-10 15:52:08

David

Thanks Alan


2017-05-10 02:58:55

Alan Elston

Hi David
The copy Destination way used here is a sort of quick way. Sometimes the exact format copied may vary.
You are using here a Copy Paste way.
In all those ways you cannot easily control the format of what you finally get.
You need to read up on the Range.PasteSpecial Method

Then you do such an operation in two lines rather than one.
The first uses the same Range Copy Method, as in the code Allen has
rngSource.Copy

But then you miss out the paste bit and you would have a code line instead looking something like this
rngTarget.PasteSpecial Paste:= xlPasteValues

Alan


2017-05-09 15:32:24

David

How do you modify this to copy/paste values instead of formulas?


2017-02-28 07:10:56

zeddy

This macro doesn't "transfer" the data from one sheet to another.
It "copies" the data.
When you "transfer" data, you move it from one place to another i.e. you don't have copies in two places.


2016-09-19 12:11:39

Debbie

I am copying filtered data from a table to a table. Sometimes it works properly, and sometimes what I past is NOT the same data I copied. It will be the same number of lines, but not the same data. The only work-around I have found is to convert the second table to a range again.


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.