Microsoft Excel is not Working - Crashes on VBA SaveAs

Jeckford

New Member
Joined
Jan 4, 2015
Messages
8
For some reason, my excel keeps crashing every time it runs over the saveas section of my code. I've written the code two different ways, and both crashes the excel. The line of code doesn't always crash excel, it is usually the second or third time running the code that it happens. Does anyone know what's wrong or why this is happening? Any help would be greatly appreciated.

Code:
Private Sub saveEdittedData()
    If Not errorMessage = "" Then Exit Sub

    DS.Visible = True
    Dim targetRow As Long
    targetRow = 1
    
    Do While Not DS.Cells(targetRow, 3) = ""
        targetRow = targetRow + 1
    Loop
    Application.CutCopyMode = False
    DS.Rows(1).Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
    
    DS.Cells(1, 1) = targetRow
    DS.Cells(1, 2) = 7
    Dim targetWorkbook As Workbook
    Set targetWorkbook = Workbooks.Add
    DS.Copy Before:=targetWorkbook.Sheets(1)
    'targetWorkbook.SaveAs targetCoreFilePath & "\MaterialsOfInterest.xlsx" '// CRASHES HERE
    targetWorkbook.SaveAs Filename:=targetCoreFilePath & "\MaterialsOfInterest.xlsx", FileFormat:=xlOpenXMLWorkbook, CreateBackup:=False '// AND CRASHES HERE
    targetWorkbook.Close
    
    DS.Visible = False

End Sub
 

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.
you haven't defined what DS is nor have you declared it as a variable
The same comments apply also to targetCoreFilePath
 
Upvote 0
Hi Michael, they have been declared, they are global variables which were declared in a separate function. Sorry for the confusion.
 
Upvote 0
Ok....in that case make sure the fpath complys with Excel save parameters....AND that it's available to save to.
Have you also tried putting the declarations in this code as a test
AND tried saving to a different location...eg C:\temp\....or the desktop
 
Upvote 0

Forum statistics

Threads
1,215,045
Messages
6,122,830
Members
449,096
Latest member
Erald

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