Copy data from another workbook into current workbook when the path and filename can change

KarenXL

New Member
Joined
May 24, 2019
Messages
6
I have a workbook that creates pivot tables and charts from data that changes weekly. The data is downloaded from a customer database and exported into its own Excel spreadsheet. I am trying to copy data from that spreadsheet into a worksheet in my pivot table workbook. The name and location of that report file can change depending on who is running the excel sheet.

I believe the code will be something like this - but I need to figure out how to obtain the path and name of the workbook I am copying from. My workbook is called leaderboard and the sheet I am copying into is the DataModel sheet. The excel sheet created by the customer database has a worksheet called DataToImport that I want to pull data from.

Worksheets("DataModel").Range("A1:U1000").Value = Workbook("need to get path and name").Worksheets("DataToImport").Range("A1:U1000").Value

I'm assuming I use application.getopenfilename to obtain the path and name of the workbook, and that I place it into a variable. But I'm not clear as to what type of variable this would be (is there a workbook variable?) and exactly how to write the statement.

Any help would be appreciated.
 

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.
Hi & welcome to MrExcel
How about
Code:
Sub KarenXL()
   Dim Wbk As Workbook
   Dim Fname As String
   
   Fname = Application.GetOpenFilename
   Set Wbk = Workbooks.Open(Fname)
   
   ThisWorkbook.Sheets("dataModel").Range("A1:U1000").Value = Wbk.Sheets("DataToImport").Range("A1:U1000").Value
   Wbk.Close False
End Sub
 
Upvote 0
That was exactly what I needed. Thank you so much. Us newbies can spend hours trying to figure out something that others have known for years!
 
Upvote 0
You're welcome & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,214,611
Messages
6,120,509
Members
448,967
Latest member
screechyboy79

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top