Pulling data from one file to another

Ananthak275

Board Regular
Joined
Aug 22, 2020
Messages
128
Office Version
  1. 2013
Platform
  1. Windows
  2. MacOS
Hi everyone, I'm trying to create a VBA script that goes into file1 and copies the data into file2. File 1 contains the data.
The issue I'm having is file2 has more columns and not necessarily in the same order as the ones in file1. As well, the Range is wrong, it has a limit. How do i make sure it gets all the relevant rows per column in file1?

Here is my code;
Sub GetDatacClosedBook()
Dim src As Workbook

Set src = Workbooks.Open("C:\Users\Akh\Documents\file1", True, True)
ThisWorkbook.Activate
Worksheets("Sheet1").Range("B2:D3").Formula = src.Worksheets("Sheet1").Range("A1:C2").Formula

End Sub
 

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
Try this
VBA Code:
Sub GetDatacClosedBook()
Dim wssrc As Worksheet, wstgt As Worksheet
Dim src As Workbook, tgt As Workbook

Set tgt = ActiveWorkbook
Set wstgt = tgt.Sheets("Sheet1")
Set src = Workbooks.Open("C:\Users\Akh\Documents\file1", True, True)
Set wssrc = src.Sheets("Sheet1")
wssrc.Range("A1:C2").Copy
wstgt.Range("B2").PasteSpecial (xlPasteFormulas)

End Sub
 
Upvote 0

Forum statistics

Threads
1,215,049
Messages
6,122,864
Members
449,097
Latest member
dbomb1414

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