PDA

View Full Version : Solved: copy contents of one workbook into another



joeyc
07-18-2008, 01:34 AM
I have two workbooks. WorkbookA. WorkbookB. Both workbooks have sheets by the names of Sheet1, Sheet2, and Sheet3. I need a macro that does the following:

*** Copy the contents of WorkbookA Sheet1 onto WorkbookB Sheet1
*** Copy the contents of WorkbookA Sheet2 onto WorkbookB Sheet2
*** Copy the contents of WorkbookA Sheet3 onto WorkbookB Sheet3

gscarter
07-18-2008, 02:14 AM
Hi joeyc,

If you know the columns that you need to copy you can use something like:



Private Sub CopyData(path1, path2)

Set wb1 = Workbooks.Open(path1, True, True)
Set wb2 = Workbooks.Open(path2, True, True)

With ThisWorkbook.Worksheets("Sheet Name")
.Columns("A:B").Value = wb1.Worksheets(Sheet Name).Columns("A:B").Value
.Columns("D:E").Value = wb2.Worksheets(Sheet Name).Columns("A:B").Value
End With

End Sub

Where the columns you wish to copy are separated by a semi-colon, and, path1 and path2 are the filepaths of the workbooks you wish to copy from.

As for copying the whole workbook, im not sure.

Gary

joeyc
07-18-2008, 02:23 AM
Let me take a look at this. I will be back.

I am on Eastern Standard Time by the way.

joeyc
07-18-2008, 08:57 AM
That doesn't seem to work.

mdmackillop
07-18-2008, 10:28 AM
Sub CopySheets()
Dim Arr, a
Arr = Array("Sheet1", "Sheet2", "Sheet3")
For Each a In Arr
Workbooks("Book1.xls").Sheets(a).Cells.Copy _
Workbooks("Book2.xls").Sheets(a).Range("A1")
Next
End Sub

joeyc
07-18-2008, 03:57 PM
Works perfectly.

:yes

mdmackillop
07-19-2008, 09:57 AM
A utlility (http://vbaexpress.com/forum/showthread.php?t=20989) I've been working on.

joeyc
07-19-2008, 10:11 AM
I will check it out. Thank you for letting me know.