VBA – 运行时错误1004 – 文档可能无法保存

我得到的实际错误是

“运行时错误”1004“:

文档没有保存。“

然后当我点击debugging时,我需要线路

ActiveWorkbook.Save 

我有一个巨大的VBAmacros,我有一些问题。 这个macros开始于大约75页左右的大型工作簿。 它从第一页开始,读取工作表的名称以确定在数据上运行哪个模型,创build新工作簿,进行计算并创buildgraphics,然后在移至下一个input工作表之前保存工作簿。 所有这些工作正常,除了保存新的工作簿。 有4个不同的模型,我正在运行,其中2个保存正确。 但是,对于其他2我随时面临着这个错误,我试着保存它们。

 OutputFolder = "location of output file" If Dir(OutputFolder, vbDirectory) = "" Then MkDir OutputFolder End If Workbooks.Open Filename:="location of input file" '(this is a path to the file) Workbooks.Open Filename:="location of lookup table" '(another path) number_of_reports = Workbooks(InputFile).Sheets.Count Workbooks(InputFile).Activate For i = 1 To number_of_reports ReportID = ActiveSheet.Name 'get 3 part ID ID_Lookup = Split(ActiveSheet.Name, "-") 'seperate into individual parts Workbooks("LookupTable").Activate 'use lookup table Sheets("CompanyLookup").Select CompanyID = Range("A" & (ID_Lookup(0) + 1)) CompanyName = Range("B" & (ID_Lookup(0) + 1)) Sheets("CategoryLookup").Select CategoryID = Range("A" & (ID_Lookup(1) + 1)) CategoryName = Range("B" & (ID_Lookup(1) + 1)) Sheets("ModelLookup").Select ModelID = Range("A" & (ID_Lookup(2) + 1)) ModelName = Range("B" & (ID_Lookup(2) + 1)) Workbooks(InputFile).Activate y = Range("B1").End(xlToRight).Column Workbooks(InputFile).Sheets(ReportID).Select 'name of the workbook SaveID = CategoryName & "_" & ModelName & "_" & FileID 'create company folder if not there already Output = "location of output" & CompanyName If Dir(Output, vbDirectory) = "" Then MkDir Output End If 'create model folder if not there already Output = "location of put" & CompanyName & "\" & ModelName If Dir(Output, vbDirectory) = "" Then MkDir Output End If 'where to save workbook Dim SaveLocation As String SaveLocation = Output & "\" & SaveID & ".xlsx" 'save workbook Dim NewBook As Workbook 'create WB for output Set NewBook = Workbooks.Add With NewBook .Title = ReportID .Subject = ReportID .SaveAs Filename:=SaveLocation End With 'decide which model to run If ModelID = 1 Then Call Regression ElseIf ModelID = 2 Then Call VolMix ElseIf ModelID = 3 Then Call ProdMix ElseIf ModelID = 4 Then Call AvgPrice End If Workbooks(SaveID).Activate Range("A1").Select deletecheck = Workbooks(SaveID).Sheets.Count 'delete extra sheets If deletecheck > 3 Then Call DeleteSheet("Sheet1") Call DeleteSheet("Sheet2") Call DeleteSheet("Sheet3") End If ActiveWorkbook.Save 'THIS IS WHERE I'M GETTING THE ERROR ActiveWorkbook.Close Workbooks(InputFile).Sheets(ReportID).Activate If i <> number_of_reports Then ActiveSheet.Next.Select End If Next i 

所以模型2和模型4是在试图保存的时候给我的问题,其他的则不是。 我已经看了每个模型的代码,我找不到4之间的任何区别,这将导致这一点,但显然是有些错误。 任何想法或build议?