Solved

Copy data from one Excel file into another using a macro

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

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 80

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

A2 = A1 That kind of cell reference is relative.  If you copy it from A2 to B2, then B2 will get this: B2 = B1 That's all fine and good, but if you then insert a new row above row 2, you'll find: A3 = A1 B3 = B1 This is intentional. …
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…
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.
This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.

707 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

14 Experts available now in Live!

Get 1:1 Help Now