Help copy sheet to closed workbook

chichaspowa

New Member
Joined
Sep 6, 2010
Messages
46
Good afternoon,

Was wondering if someone could help me out with a piece of code.

What i need to do is when closing the workbook i would like to copy the sheet called "registos",

and paste it on the sheet "registos" in the closed workbook ... \\server\server\Dest1.xls...


thanks
 

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.
not tested
Code:
Private Sub Workbook_BeforeClose(Cancel As Boolean)
Set wb = ThisWorkbook
Set wb1 = Workbooks.Open("\\server\server\Dest1.xls")
wb1.Sheets("registos").Delete
wb.Sheets("registos").Copy after:=wb1.Sheets(Sheets.Count)
wb1.Close True
End Sub
 
Upvote 0
getting runtime error 9, out of range on this line ........... wb.Sheets("registos").Copy after:=wb1.Sheets(Sheets.Count)

funny enough i had tried that code already, it works fine if i wanna fetch data from the closed file and not paste into the closed file.

I use it to fetch data on open book event.


Private Sub Workbook_Open()





Dim ws As Worksheet
Dim targetWorkbook As Workbook, wb As Workbook



Set targetWorkbook = Application.ActiveWorkbook



Application.DisplayAlerts = False


Set wb = Workbooks.Open("\\server\server\Dest1.xls")


targetWorkbook.Sheets("Registos").Delete


wb.Sheets("Registos").Copy after:=targetWorkbook.Sheets(targetWorkbook.Sheets.Count)

wb.Close


main.Show




End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,215,898
Messages
6,127,632
Members
449,391
Latest member
Kersh82

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