2

我得到的实际错误是

“运行时错误‘1004’:

文档未保存。”

然后当我点击调试时,它会把我带到这条线上

ActiveWorkbook.Save

我有一个巨大的 VBA 宏,但我遇到了一些问题。宏从一个巨大的工作簿开始,其中包含大约 75 个数据表。它从第一页开始,读取工作表的名称以确定要在数据上运行的模型,创建一个新工作簿,进行计算并创建图形,然后在继续下一个输入工作表之前保存工作簿。除了保存新工作簿之外,所有这些都可以正常工作。我正在运行 4 种不同的模型,其中 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 个模型之间的任何区别,但显然有问题。有什么想法或建议吗?

4

0 回答 0