VBA - 运行时错误1004 - 无法保存文档

时间:2012-06-22 13:29:08

标签: excel vba runtime-error save

我得到的实际错误是

“运行时错误'1004':

文档未保存。“

然后当我点击调试时,它会把我带到

ActiveWorkbook.Save

我有一个巨大的VBA宏,我遇到了一些问题。宏是在一个包含约75张左右数据的巨型工作簿中启动的。它从第一页开始,读取工作表的名称以确定在数据上运行哪个模型,创建新工作簿,执行计算并创建图形,然后在转到下一个输入表之前保存工作簿。除了保存新工作簿之外,所有这些都可以正常工作。我正在运行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个代码之间没有任何区别,但显然出现了问题。任何想法或建议?

0 个答案:

没有答案