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