Copying sheet from external file

adrianadik

New Member
Joined
Aug 28, 2014
Messages
4
Hi all,

I'm trying to create VBA code which opens file from external drive, than copies selected sheet and pastes it into active workbook. That's what I've done so far:

Code:
Sub CopyWorksheet(control As IRibbonControl)

Application.ScreenUpdating = False
Application.DisplayAlerts = False


Dim wbSource As Workbook, wbDest As Workbook
Dim wsSource As Worksheet, wsDest As Worksheet


Set wbSource = Workbooks.Open("FILE PATH\file.xlsx", , True)
Set wsSource = wbSource.Worksheets(1)
Set wbDest = ThisWorkbook
Set wsDest = Sheets(1)


wbSource.ws.Source.Copy wbDest.wsDest


wbSource.Close


Application.ScreenUpdating = True
Application.DisplayAlerts = True


End Sub

Code opens workbook but (I suppose) it treats already opened workbook (source) as 'this workbook'.

Is there any way to copy sheet from opened file?

Regards from Poland
 

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
Hi,

this should work

Code:
Sub CopyWorksheet()

Application.ScreenUpdating = False
Application.DisplayAlerts = False

Dim wbSource As Workbook, wbDest As Workbook
Dim wsSource As Worksheet, wsDest As Worksheet

Set wbDest = ThisWorkbook
Set wsDest = wbDest.Sheets(1)
Set wbSource = Workbooks.Open("[COLOR=#574123]FILE PATH\[/COLOR]\file.xlsx", , True)
Set wsSource = wbSource.Sheets(1)

wsSource.Copy wsDest

wbSource.Close

Application.ScreenUpdating = True
Application.DisplayAlerts = True

End Sub

regards from Slovakia
 
Upvote 0
Dakujem wist! Works perfect :)

I got one more issue. Macro is controlled by custom ribbon.

When user will open new excel window (and will not edit anything) code will open external file in this window - this will crash macro.

Is there any way to force opening file in new (seperate?) window to avoid this bug?

Regards,
 
Upvote 0

Forum statistics

Threads
1,214,823
Messages
6,121,779
Members
449,049
Latest member
greyangel23

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