Solved

Copy data from one Excel file into another using a macro

Posted on 2014-01-25
3
16,605 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 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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

Some code to ensure data integrity when using macros within Excel. Also included code that helps secure your data within an Excel workbook.
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 will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

617 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