Macro to copy and paste between workbooks

Bobbrown

New Member
Joined
Jul 19, 2011
Messages
14
I have two Workbooks one named master and the other slave.
I have created a macro to copy a range of data from a sheet named DATA in the master workbook to a sheet called DataCopyMaster in the slave workbook and then make the Records sheet active before saving and closing the slave workbook, which all works fine. I wanted to add this to the BeforeClose event of the master workbook so I can ensure the slave updates every time the master is closed but nolonger works once added to the BeforeClose event.
<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p> </o:p>
When I close the Master workbook I get a Visual basic runtime error runtime error ‘9’
Script out of range
It appears to code runs to the line:
Code:
Sheets("DataCopyMaster").Select
<o:p> </o:p>

Below is the macro I'm using in full.
Code:
Private Sub Workbook_BeforeClose(Cancel As Boolean)
    Range("A1:M50").Select
    Selection.Copy
    ChDir "C:\DATA\My Documents"
    Workbooks.Open Filename:="C:\DATA\My Documents\TestSheetSlave.xls"
    Sheets("DataCopyMaster").Select
    Range("A1:M50").Select
    ActiveSheet.Paste
    Range("A1").Select
    Sheets("Records").Select
    ActiveWorkbook.Save
    ActiveWindow.Close
    Application.CutCopyMode = False
    Range("A1").Select
End Sub
<o:p> </o:p>
<o:p> </o:p>
I don’t have a lot of experience with coding and I am sure I’m missing something simple here but would appreciate any assistance.

TB.
 

Excel Facts

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.

Forum statistics

Threads
1,224,586
Messages
6,179,718
Members
452,939
Latest member
WCrawford

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