open excel file and copy data to another file

N

Norvascom

I would like to create a macro that would do the following:

1- Open a closed Excel file located on my drive named "A" and select
the sheet named "Sheet1"
2- Copy all data from this sheet starting on cell A1 to the already
opened master Excel file named "B" on sheet named "Sheet2" cell A1.

Thanks in advance for your help.
 
N

Norvascom

Try the macro recorder.

Gord Dibben  MS Excel MVP





- Show quoted text -

I does not work because the file to be opened is not always located in
the same place on the drive.
 
R

Ryan H

This macro will prompt the user to select where the file is, open it, copy
the data on Sheet1 to the master workbook Sheet2. I assume you will want to
call this sub from a command button in your master excel sheet. Hope this
helps! If so, let me know, click "YES" below.

Sub ImportSheet()

Dim wbkData As Variant

' select file with data to be opened
wbkData = Application.GetOpenFilename

' open the workbook
If wbkData = False Then
MsgBox "You didn't select a workbook.", vbInformation
Else
Workbooks.Open wbkData
Set wbkData = ActiveWorkbook
End If

' copy data to master workbook
ThisWorkbook.Sheets("Sheet2").Cells = wbkData.Sheets("Sheet1").Cells

' close the workbook without saving
wbkData.Close

End Sub
 
G

Gord Dibben

Would have been handy if you mentioned that first time around.

But I see Ryan has posted something for you.


Gord
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top