Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Copy data from one Excel file into another using a macro

Posted on 2014-01-25
3
Medium Priority
?
17,048 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 27

Accepted Solution

by:
MacroShadow earned 1000 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 1000 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This article describes a serious pitfall that can happen when deleting shapes using VBA.
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.

670 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