我需要在excel模型中运行多个场景。在当前模型中,每个方案都必须手动加载,每次运行需要3个多小时。我还需要将模型结果保存在新工作簿中。
我正在编写一个宏,以允许场景自动加载到模型中,运行并保存为新工作簿。我的代码如下。问题是它何时进入代码行" Application.run ...",运行时错误1004:应用程序定义或对象定义错误。
请帮忙!
Private Sub CommandButton1_Click()
Dim ScentoRun, Path, N As String
Dim DestCom, Target As Range
Dim SCount, x As Integer
Path = "F:\"
SCount = Workbooks("Scenarios to Run").Worksheets("Sheet1").Cells(6, Columns.Count).End(xlToLeft).Column
For x = 1 To SCount
Workbooks.Open Filename:=Path & "The Model.xlsm"
Workbooks("Scenarios to Run").Worksheets("Sheet1").Columns(x).Copy
Workbooks("The Model").Worksheets("Scenarios").Columns(6).PasteSpecial
ScentoRun = Workbooks("The Model").Worksheets("Scenarios").Range("F6").Value
Application.DisplayAlerts = False
Workbooks("The Model").SaveAs Filename:=Path & ScentoRun, FileFormat:=52
Application.DisplayAlerts = True
Workbooks(ScentoRun).Worksheets("Results").Range("F8") = Workbooks(ScentoRun).Worksheets("Scenarios").Range("F6")
Workbooks(ScentoRun).Activate
N = Workbooks(ScentoRun).name
Application.Run "'N'!loadScenario" '----- ERROR HERE
Workbooks(ScentoRun).Save
Workbooks(ScentoRun).Close (True)
Next x
End Sub
答案 0 :(得分:3)
Application.Run "'" & N & "'!loadScenario" '----- ERROR HERE