Solved

Copy data from one Excel file into another using a macro

Posted on 2014-01-25
3
15,231 Views
Last Modified: 2014-01-29
Let's say I am in Sheet1 of the target Excel file named File1.  I need a macro that will open File2, copy the contents of cell H4 in Sheet3 of File2 to cell B2 in Sheet1 of File1 and copy the contents of cell J10 in Sheet3 of File2 to cell B3 in Sheet1 of File1 and then close File2 and Save the changes made to File1. Ideally, I'd like to be able to press a command button or some other control to run the macro.

By the way, I'm presuming File2 has to be opened first in order to copy from it. That's why I indicated the open and close steps above. If those steps are not necessary, then skip. I'm just trying to copy data from and to specific locations (cell addresses) between two Excel files.
0
Comment
Question by:dbfromnewjersey
3 Comments
 
LVL 27

Accepted Solution

by:
MacroShadow earned 250 total points
ID: 39809726
There is more than one way to skin a cat but I think this is the best method:
Sub Demo()

    Dim wbSource As Workbook
    Dim wbTarget As Workbook

    ' First open both workbooks :
    Set wbSource = Workbooks.Open("  ") ' <<< path to source workbook
    Set wbTarget = ActiveWorkbook ' Workbooks.Open("  ") ' <<< path to destination workbook

    'Now, transfer values from wbSource to wbTarget:
    wbTarget.Sheets("Sheet1").Range("B2").Value = wbSource.Sheets("Sheet3").Range("H4")
    wbTarget.Sheets("Sheet1").Range("B3").Value = wbSource.Sheets("Sheet3").Range("J10")

    'Close source:
    wbSource.Close

End Sub

Open in new window

0
 
LVL 81

Assisted Solution

by:byundt
byundt earned 250 total points
ID: 39809738
If you don't want to open File2, you can get values from its cells using worksheet formulas and then replace those formulas with the values returned. The code below also saves File1 as requested.
Sub GetData()
Dim flPath As String, flName As String, frmla As String, shtName As String
flPath = "X:\VBA\Sample '13\"
flName = "Book2.xlsm"
shtName = "Sheet3"
frmla = Replace(flPath & "[" & flName & "]" & shtName, "'", "''")
With Worksheets("Sheet1")
    .Range("B2").Formula = "='" & frmla & "'!H4"
    .Range("B2").Formula = .Range("B2").Value
    .Range("B3").Formula = "='" & frmla & "'!J10"
    .Range("B3").Formula = .Range("B3").Value
End With
ActiveWorkbook.Save
End Sub

Open in new window

0
 

Author Comment

by:dbfromnewjersey
ID: 39817436
Thank you.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

INDEX and MATCH can be used to great effect to replace HLOOKUP and VLOOKUP as it does not have the limitation of needing the data to be sorted so that the reference value is in the first column or row. It also has the ability to perform a bi-directi…
This article descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
The viewer will learn how to use the =DISCRINV command to create a discrete random variable, use this command to model a set of probabilities and outcomes in a Monte Carlo simulation, and learn how to find the standard deviation of a set of probabil…
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.

948 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now