ActiveWorkbook.SaveCopyAs crashes excel on 2. run

STGE2

New Member
Joined
Oct 8, 2009
Messages
17
Hi<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:eek:ffice:eek:ffice" /><o:p></o:p>
<o:p></o:p>
I’m working on an excel workbook where I need to take out 2 worksheets and save as another workbook.<o:p></o:p>
<o:p></o:p>
The code below runs fine the first time the macro is used in the workbook, but the second time excel craches! I don’t get an excel error report, only the standard Windows error screen:<o:p></o:p>
<o:p></o:p>
“Microsoft Office Excel has encountered a problem and needs to close. We are sorry for the inconvenience.”<o:p></o:p>
I can then as standard choose “Send error report” or “Don’t send”<o:p></o:p>
<o:p></o:p>
If I view the contents of the error report I get the following error signature:<o:p></o:p>
<o:p></o:p>
AppName: excel.exe AppVer: 11.0.8169.0 AppStamp:465f27bd<o:p></o:p>
ModName: excel.exe ModVer: 11.0.8169.0 ModStamp:465f27bd<o:p></o:p>
fDebug: 0 Offset: 000c0cee<o:p></o:p>
<o:p></o:p>
To determine where excel crashes I have put in some Msgboxes. When I run the macro the 2. time I only see the message “saveAs” and don’t get to the message “close”.<o:p></o:p>
<o:p></o:p>
Code:
[COLOR=black][FONT=Verdana]<o:p></o:p>[/FONT][/COLOR]
[COLOR=black][FONT=Verdana]Sub CopySheetsToNewWorkbook(ws As Worksheet)<o:p></o:p>[/FONT][/COLOR]
[COLOR=black][FONT=Verdana]  Dim NewName As String<o:p></o:p>[/FONT][/COLOR]
[COLOR=black][FONT=Verdana]  Dim nm As Name<o:p></o:p>[/FONT][/COLOR]
[COLOR=black][FONT=Verdana]  Dim sh As Shape<o:p></o:p>[/FONT][/COLOR]
[COLOR=black][FONT=Verdana]       <o:p></o:p>[/FONT][/COLOR]
[COLOR=black][FONT=Verdana]  Application.ScreenUpdating = False<o:p></o:p>[/FONT][/COLOR]
[COLOR=black][FONT=Verdana]       <o:p></o:p>[/FONT][/COLOR]
[COLOR=black][FONT=Verdana]      ' Copy specific sheets<o:p></o:p>[/FONT][/COLOR]
[COLOR=black][FONT=Verdana]      On Error GoTo ErrHandler<o:p></o:p>[/FONT][/COLOR]
[COLOR=black][FONT=Verdana]      sheets(Array("FrontPage", ws.Name)).Copy<o:p></o:p>[/FONT][/COLOR]
[COLOR=black][FONT=Verdana]      On Error GoTo 0<o:p></o:p>[/FONT][/COLOR]
[COLOR=black][FONT=Verdana]                                                          <o:p></o:p>[/FONT][/COLOR]
[COLOR=black][FONT=Verdana]      'Remove buttons<o:p></o:p>[/FONT][/COLOR]
[COLOR=black][FONT=Verdana]      For Each sh In sheets(2).Shapes<o:p></o:p>[/FONT][/COLOR]
[COLOR=black][FONT=Verdana]          sh.Delete<o:p></o:p>[/FONT][/COLOR]
[COLOR=black][FONT=Verdana]      Next<o:p></o:p>[/FONT][/COLOR]
[COLOR=black][FONT=Verdana]                         <o:p></o:p>[/FONT][/COLOR]
[COLOR=black][FONT=Verdana]      ' Remove named ranges<o:p></o:p>[/FONT][/COLOR]
[COLOR=black][FONT=Verdana]      For Each nm In ActiveWorkbook.Names<o:p></o:p>[/FONT][/COLOR]
[COLOR=black][FONT=Verdana]          nm.Delete<o:p></o:p>[/FONT][/COLOR]
[COLOR=black][FONT=Verdana]      Next nm<o:p></o:p>[/FONT][/COLOR]
[COLOR=black][FONT=Verdana]               <o:p></o:p>[/FONT][/COLOR]
[COLOR=black][FONT=Verdana]      'Make dir to save TS<o:p></o:p>[/FONT][/COLOR]
[COLOR=black][FONT=Verdana]      On Error Resume Next<o:p></o:p>[/FONT][/COLOR]
[COLOR=black][FONT=Verdana]      MkDir (ThisWorkbook.path & "\TS")<o:p></o:p>[/FONT][/COLOR]
[COLOR=black][FONT=Verdana]      On Error GoTo 0<o:p></o:p>[/FONT][/COLOR]
[COLOR=black][FONT=Verdana]                       <o:p></o:p>[/FONT][/COLOR]
[COLOR=black][FONT=Verdana]      ' Save it with the NewName and in the same directory as original<o:p></o:p>[/FONT][/COLOR]
[COLOR=black][FONT=Verdana]MsgBox "saveAs"<o:p></o:p>[/FONT][/COLOR]
[COLOR=black][FONT=Verdana]      ActiveWorkbook.SaveCopyAs ThisWorkbook.path & "\TS\" & ws.Name & "_" & ws.Range("I4") & ".xls"<o:p></o:p>[/FONT][/COLOR]
[COLOR=black][FONT=Verdana]MsgBox "close"<o:p></o:p>[/FONT][/COLOR]
[COLOR=black][FONT=Verdana]      ActiveWorkbook.Close SaveChanges:=False<o:p></o:p>[/FONT][/COLOR]
[COLOR=black][FONT=Verdana]<o:p></o:p>[/FONT][/COLOR]
[COLOR=black][FONT=Verdana]      'Select the initial worksheet in initial workbook<o:p></o:p>[/FONT][/COLOR]
[COLOR=black][FONT=Verdana]      ws.Select<o:p></o:p>[/FONT][/COLOR]
[COLOR=black][FONT=Verdana]       <o:p></o:p>[/FONT][/COLOR]
[COLOR=black][FONT=Verdana]      Application.ScreenUpdating = True<o:p></o:p>[/FONT][/COLOR]
[COLOR=black][FONT=Verdana]  Exit Sub<o:p></o:p>[/FONT][/COLOR]
[COLOR=black][FONT=Verdana]   <o:p></o:p>[/FONT][/COLOR]
[COLOR=black][FONT=Verdana]ErrHandler:<o:p></o:p>[/FONT][/COLOR]
[COLOR=black][FONT=Verdana]  MsgBox "Specified sheets do not exist within this workbook"<o:p></o:p>[/FONT][/COLOR]
[COLOR=black][FONT=Verdana]End Sub<o:p></o:p>[/FONT][/COLOR]
<o:p></o:p>
 

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college

Forum statistics

Threads
1,214,954
Messages
6,122,462
Members
449,085
Latest member
ExcelError

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