automation error exception occurred

kyddrivers

Board Regular
Joined
Mar 22, 2013
Messages
59
Office Version
  1. 365
Platform
  1. Windows
I am running into an issue with several macros that export tabs into a separate workbook, breaks links, copy/paste values, save as the workbook and then closes.</SPAN>

Code:
 Sheets("Site").Select
    Sheets("Site").Copy
    ActiveWorkbook.BreakLink Name:= _
        "[URL="file://\\path"]\\path[/URL]\file.xlsm", Type:= _
        xlExcelLinks
    Cells.Select
    Range("C1").Activate
    Selection.Copy
    Selection.PasteSpecial Paste:=xlPasteValuesAndNumberFormats, Operation:= _
        xlNone, SkipBlanks:=False, Transpose:=False
    Range("C1").Select
    
    Sheets("Site").Select
    ActiveWorkbook.SaveAs Filename:= _
        "[URL="file://\\P"]\\[/URL]path\Site.xlsx" _
        , FileFormat:=xlOpenXMLWorkbook, CreateBackup:=False
        
    Application.CutCopyMode = False
    ActiveWorkbook.Close

The error forces excel to close completely and seems to happen about the time I am doing the save as.

Any words of wisdom to correct this issue is greatly appreciated
 

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
if you comment out the save-as, does it then work OK? Also when you then save manually?

Why are you selecting so much? and why are you copying the sheet over itself?

This whole thing can be written much more compact:
Code:
Sub Macro1()    Dim sName As String
    
   ' get name from original file
    sName = ActiveWorkbook.Name
   'copy sheet to new workbook and break links
    Sheets("Site").Copy
    ActiveWorkbook.BreakLink Name:=sName, Type:=xlExcelLinks
   'save new file
    ActiveWorkbook.SaveAs Filename:= _
        "\\path\Site.xlsx" _
        , FileFormat:=xlOpenXMLWorkbook, CreateBackup:=False
        
    ActiveWorkbook.Close


End Sub
 
Upvote 0
I can do everything manually.

The reason to copy and paste on to itself is to remove all of the formulas before publishing the data.

I will give this a try and see if it fixes the problem.

Thanks!!

if you comment out the save-as, does it then work OK? Also when you then save manually?

Why are you selecting so much? and why are you copying the sheet over itself?

This whole thing can be written much more compact:
Code:
Sub Macro1()    Dim sName As String
    
   ' get name from original file
    sName = ActiveWorkbook.Name
   'copy sheet to new workbook and break links
    Sheets("Site").Copy
    ActiveWorkbook.BreakLink Name:=sName, Type:=xlExcelLinks
   'save new file
    ActiveWorkbook.SaveAs Filename:= _
        "\\path\Site.xlsx" _
        , FileFormat:=xlOpenXMLWorkbook, CreateBackup:=False
        
    ActiveWorkbook.Close


End Sub
 
Upvote 0
instead of copy then use
Code:
ActiveSheet.UsedRange.Value = ActiveSheet.UsedRange.Value
Copy is a slow item.
 
Upvote 0
instead of copy then use
Code:
ActiveSheet.UsedRange.Value = ActiveSheet.UsedRange.Value
Copy is a slow item.

I implemented the ActiveSheet.UsedRange.Value to get rid of the copy and paste values. Excel is still crashing when I get to the save as. I do not get the exception error pop-up box anymore, just excel has encountered an error and must close.
 
Upvote 0
What happens if you try this on a fresh workbook. Just make something small with a link to another sheet and run the code. With me that works OK, no crash. It could be that your sheet has another problem.

If a fresh sheet works OK then use it to copy ranges from the old workbook into, not everything at once. See if it crashes at some point, then you can pinpoint the issue better.

I used to have issues like that , and just creating a new sheet and copying things across would solve it.
 
Upvote 0
What happens if you try this on a fresh workbook. Just make something small with a link to another sheet and run the code. With me that works OK, no crash. It could be that your sheet has another problem.

If a fresh sheet works OK then use it to copy ranges from the old workbook into, not everything at once. See if it crashes at some point, then you can pinpoint the issue better.

I used to have issues like that , and just creating a new sheet and copying things across would solve it.

I was doing some work on this...I think I found the issue with the save as piece. It appears that the mapped drive was not connected to the PC running the macro.
:biggrin:
Thanks for your help sijpie!!
 
Upvote 0
I thought I had solved the crashing. I have one snipet of code that will crash when I run it. I stepped through the macro and it runs fine, but if I run it as a whole, it crashes. When I commented out the save as piece the macro still crashes, so I can not pin point where the crash happens.

Very Frustrating....:mad:

Thanks!
 
Upvote 0

Forum statistics

Threads
1,213,496
Messages
6,113,993
Members
448,539
Latest member
alex78

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