Copy data from new workbook into existing one

ai1094

Board Regular
Joined
Aug 23, 2018
Messages
92
Hi,

I have workbook. I want to run a macro that let's me select an excel file from my computer and copy information from the selected file into a new sheet of my existing workbook. Is there a simple VBA code to do this?


Thanks.
 

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
Ok say I have a workbook open right now. When I run the macro I want a popup box to come up and let me select an excel file from my computer. Once that excel file is selected, just copy all contents within the sheet into a new sheet of my already opened workbook. I hope that makes it a little more clear.
 
Upvote 0
How about
Code:
Sub ai1094()
   Dim Fname As String
   Dim Wbk1 As Workbook, Wbk2 As Workbook
   
   Set Wbk1 = ActiveWorkbook
   Fname = Application.GetOpenFilename
   Set Wbk2 = Workbooks.Open(Fname)
   Wbk2.Sheets(1).Copy Wbk1.Sheets(1)
   Wbk2.Close False
End Sub
 
Upvote 0
This works. However, I have a macro that is creating a pivot table within that existing workbook and I think it may be referencing the new sheet that was added, which is not what I want it to do. Can I fix this?

How about
Code:
Sub ai1094()
   Dim Fname As String
   Dim Wbk1 As Workbook, Wbk2 As Workbook
   
   Set Wbk1 = ActiveWorkbook
   Fname = Application.GetOpenFilename
   Set Wbk2 = Workbooks.Open(Fname)
   Wbk2.Sheets(1).Copy Wbk1.Sheets(1)
   Wbk2.Close False
End Sub
 
Upvote 0
Maybe
Code:
Sub ai1094()
   Dim Fname As String
   Dim Wbk1 As Workbook, Wbk2 As Workbook
   
   Set Wbk1 = ActiveWorkbook
   Fname = Application.GetOpenFilename
   Set Wbk2 = Workbooks.Open(Fname)
   Wbk2.Sheets(1).Copy , Wbk1.Sheets(Wbk1.Sheets.Count)
   Wbk2.Close False
End Sub
 
Upvote 0
Ok I think I got it to work. I'll be back if I have any more questions. Thanks a lot!!
 
Upvote 0
You're welcome & thanks for the feedback
 
Upvote 0
Quick question, once the new worksheet is copied into my existing workbook, is there a way I can change the sheet name to "REF" ?
 
Upvote 0
Try
Code:
Sub ai1094()
   Dim Fname As String
   Dim Wbk1 As Workbook, Wbk2 As Workbook
   
   Set Wbk1 = ActiveWorkbook
   Fname = Application.GetOpenFilename
   Set Wbk2 = Workbooks.Open(Fname)
   Wbk2.Sheets(1).Copy , Wbk1.Sheets(Wbk1.Sheets.Count)
   Wbk2.Close False
   Wbk1.Sheets(Wbk1.Sheets.Count).Name = "Ref"
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,583
Messages
6,120,383
Members
448,956
Latest member
JPav

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