Copy data from another workbook

militambe3171

New Member
Joined
Jun 27, 2015
Messages
11
Hi,

I want to create simple excel report for that data will be copied from different workbook, but i want to copy specific columns from each workbook. data will be match based on column names and will be pasted accordingly

For ex.

Report file column names are
A:Name
B:DOB
C:Last Name
D:Department
E:DOJ

Data contains in file 1
(Copy below columns from file 1)

A:Name
B:Last Name
C:Department

Data contains in file 2
(Copy below columns from file 2)

A:DOB
B:DOJ


file located at different locations


Please let me know if you need more details

Thanks in advance
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
This will do it,but you need to be sure that the data in each row from file 1 will match the data in each row from file 2. I assume you are copying to and from Sheet1. You will need to change the sheet names.

Code:
Sub CopyData()
Dim wbkFile1 As Workbook
Dim wbkFile2 As Workbook
Dim sFile1 As String
Dim sFile2 As String


sFile1 = Application.GetOpenFilename
Set wbkFile1 = Workbooks.Open(sFile1)
wbkFile1.Sheets("Sheet1").Range("A:C").Copy _
    Destintion:=ThisWorkbook.Sheets("Sheet1").Range("A1")
wbkFile1.Close savechanges:=False


sFile2 = Application.GetOpenFilename
Set wbkFile2 = Workbooks.Open(sFile2)
wbkFile2.Sheets("Sheet1").Range("A:B").Copy _
    Destintion:=ThisWorkbook.Sheets("Sheet1").Range("D1")
wbkFile2.Close SaveChanges:=False


End Sub

Hope this helps,
David
 
Upvote 0

Forum statistics

Threads
1,223,243
Messages
6,170,967
Members
452,371
Latest member
Frana

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top