You are here:

Excel/Copying from one workbook to another workbook.

Advertisement


Question
Hi Tom,

You wrote this following code for me last week, for copying data from one worksheet to another in the same workbook. My question is how would I change this code to now copy the data from one worksheet to another worksheet in a new workbook.

Sub abc()
Dim shDest As Worksheet, shSource As Worksheet
Dim lastrow As Long, r As Range
Set shDest = ThisWorkbook.Worksheets("MyData")
Set shSource = ThisWorkbook.Worksheets("Form1")
Set r = shSource.Range("C5:C12")
r.Copy
lastrow = shDest.Cells(shDest.Rows.Count, "C").End(xlUp).Row + 1
shDest.Cells(lastrow, "C").PasteSpecial Transpose:=True
Application.CutCopyMode = False
End Sub

Regards

M

Answer
M

You posted the code I provided in answer to the question when you asked it.  You later asked to pastevalues.  I will assume you still want to paste values
assume the other workbook is named  Otherworkbook.xlsx and it is open in the same instance of excel.  

Sub abc()
Dim shDest As Worksheet, shSource As Worksheet
Dim lastrow As Long, r As Range
Set shDest = Workbooks("OtherWorkbook.xlsx").Worksheets("MyData")
Set shSource = ThisWorkbook.Worksheets("Form1")
Set r = shSource.Range("C5:C12")
r.Copy
lastrow = shDest.Cells(shDest.Rows.Count, "C").End(xlUp).Row + 1
shDest.Cells(lastrow, "C").PasteSpecial Paste:=xlPasteValues, Transpose:=True
Application.CutCopyMode = False
End Sub



--
Regards,
Tom Ogilvy

About Excel
This topic answers questions related to Microsoft Excel spreadsheet (or workbook) stand-alone or Mircrosoft Office Excel including Excel 2003, Excel 2007, Office 2000, and Office XP. You can get Excel help on Excel formulas(or functions), Excell macros, charting in Excel, advanced features, and the general use of Excel. This does not provide a general Excel tutorial nor the basics of using a spreadsheet. It provides specific answers to using Microsoft Excel only. If you do not see your Excel question answered in this area then please ask an Excel question here

Excel

All Answers


Answers by Expert:


Ask Experts

Volunteer


Tom Ogilvy

Expertise

Selected as an Excel MVP by Microsoft since 1999. Answering Excel questions in Allexperts since its inception in 2001. Able to answer questions on almost all aspects of Excel's internal capabilities. If seeking a VBA solution, please specify that in your question itself so I give you the answer you want. [Excel has weak protection - if you are distributing an application, I don't answer questions on how to protect your project from your users.]

Experience

Extensive experience.

Education/Credentials
Master of Science (MS) degree Operations Research (ORSA)

Awards and Honors
Microsoft MVP in Excel.

©2016 About.com. All rights reserved.