Solved

Copy data from one Excel file into another using a macro

Posted on 2014-01-25
3
15,692 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction This Article is a follow-up to my Mappit! Addin Article (http://www.experts-exchange.com/A_2613.html), it was inspired by an email posting I made to EUSPRIG (http://www.eusprig.org/index.htm), I will briefly cover: 1) An overvie…
When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.

792 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