Import from different workbook

Nanogirl21

Active Member
Joined
Nov 19, 2013
Messages
287
Office Version
  1. 2013
Platform
  1. Windows
Hi,

Happy New Year!

How can I import and replace data from another workbook into the active worksheet?

Ideally, I’d like a prompt to select the import file since it will be different every time I use the VBA. The sheet name in the import file will always be named Default.
The import data would be pasted in the active sheet of the workbook starting in row 2 (row 1 is headers). All “old” data should be removed.
A prompt confirming successful import would be helpful too.

Thank you for any help.
 

Some videos you may like

Excel Facts

Can you AutoAverage in Excel?
There is a drop-down next to the AutoSum symbol. Open the drop-down to choose AVERAGE, COUNT, MAX, or MIN

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
55,433
Office Version
  1. 365
Platform
  1. Windows
How about
VBA Code:
Sub Nanogirl()
   Dim Fname As Variant
   Dim Wbk As Workbook
   Dim Ws As Worksheet
   
   Set Ws = ActiveSheet
   Fname = Application.GetOpenFilename
   If Fname = "" Then Exit Sub
   Set Wbk = Workbooks.Open(Fname)
   Ws.UsedRange.Offset(1).Clear
   Wbk.Sheets("Default").UsedRange.Offset(1).Copy Ws.Range("A2")
End Sub
 

Nanogirl21

Active Member
Joined
Nov 19, 2013
Messages
287
Office Version
  1. 2013
Platform
  1. Windows
How about
VBA Code:
Sub Nanogirl()
   Dim Fname As Variant
   Dim Wbk As Workbook
   Dim Ws As Worksheet
  
   Set Ws = ActiveSheet
   Fname = Application.GetOpenFilename
   If Fname = "" Then Exit Sub
   Set Wbk = Workbooks.Open(Fname)
   Ws.UsedRange.Offset(1).Clear
   Wbk.Sheets("Default").UsedRange.Offset(1).Copy Ws.Range("A2")
End Sub

This worked perfectly. Is there a way to close the import workbook after the task is complete?
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
55,433
Office Version
  1. 365
Platform
  1. Windows
How about
VBA Code:
Sub Nanogirl()
   Dim Fname As Variant
   Dim Wbk As Workbook
   Dim Ws As Worksheet
   
   Set Ws = ActiveSheet
   Fname = Application.GetOpenFilename
   If Fname = "" Then Exit Sub
   Set Wbk = Workbooks.Open(Fname)
   Ws.UsedRange.Offset(1).Clear
   Wbk.Sheets("Default").UsedRange.Offset(1).Copy Ws.Range("A2")
   Wbk.Close False
End Sub
 
Solution

Nanogirl21

Active Member
Joined
Nov 19, 2013
Messages
287
Office Version
  1. 2013
Platform
  1. Windows
How about
VBA Code:
Sub Nanogirl()
   Dim Fname As Variant
   Dim Wbk As Workbook
   Dim Ws As Worksheet
  
   Set Ws = ActiveSheet
   Fname = Application.GetOpenFilename
   If Fname = "" Then Exit Sub
   Set Wbk = Workbooks.Open(Fname)
   Ws.UsedRange.Offset(1).Clear
   Wbk.Sheets("Default").UsedRange.Offset(1).Copy Ws.Range("A2")
   Wbk.Close False
End Sub

Thank you so much. You are always a great help.
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
55,433
Office Version
  1. 365
Platform
  1. Windows
You're welcome & thanks for the feedback.
 

Watch MrExcel Video

Forum statistics

Threads
1,127,387
Messages
5,624,389
Members
416,026
Latest member
melvic69

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
Top