Use vba to copy data from one closed excel file to an open file

This example describes my problem (I hope):
Have two Excel files. c:\temp\test1.xlsx and c:\temp\test2.xlsx. When clicking a button in the open file "test2" I want to copy values from cells B10:D10 in the closed file "test1" to cells B2:D2 in "test2".
I know how to wite and use simple vba code, but this is a bit above my head. Appreciate any help.
hallpettAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Saurabh Singh TeotiaCommented:
Assuming you are running this from 3rd workbook then you can use the code to do this..

Sub copydata()
Application.ScreenUpdating = False
Application.DisplayAlerts = False

    Dim xpath As String, npath As String
    Dim wb As Workbook, wb1 As Workbook

    xpath = "C:\temp\"
    npath = "C:\temp\"

    Set wb = Workbooks.Open(xpath & "test1.xlsx")
    Set wb1 = Workbooks.Open(xpath & "test2.xlsx")

    wb.Activate
    Sheets("Sheet1").Range("b10:d10").Copy wb1.Sheets("Sheet1").Range("B2")
    wb.Close
    wb1.Close (True)

End Sub

Open in new window


Saurabh...
hallpettAuthor Commented:
Should have explained it better. I need to run it from workbook "test2".
Saurabh Singh TeotiaCommented:
Use this code..

Sub copydata()
Application.ScreenUpdating = False
Application.DisplayAlerts = False

    Dim xpath As String, npath As String
    Dim wb As Workbook, wb1 As Workbook

    xpath = "C:\temp\"
  Set wb1 = ThisWorkbook

    Set wb = Workbooks.Open(xpath & "test1.xlsx")
  

    wb.Activate
    Sheets("Sheet1").Range("b10:d10").Copy wb1.Sheets("Sheet1").Range("B2")
    wb.Close


End Sub

Open in new window


Saurabh...

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Excel

From novice to tech pro — start learning today.