按顺序运行多个WB宏

时间:2015-01-23 10:33:30

标签: excel vba

我有不同的工作簿,每个工作簿都有自己的宏。在第一个工作簿上运行宏后,结果用于更新第二个工作簿。现在可以打开第二个工作簿来运行自己的特定宏。第二个WB的结果现在用于更新第三个WB等

我需要按顺序在不同的工作簿中运行这些宏。最终的WB给出了最终结果。我可以通过打开每个WB&运行其特定的宏,但我更喜欢在auto上运行。所以我有一个单独的WB与宏来实现这一目标。但它在第一步之后停止,即在运行第一个WB宏之后。

主要宏如下:

Private Sub EM()
'PR closeout
Sheets("Task").Select    'This WB call all the WBs in sequence
If Range("AB2").Value = 0 Then GoTo 10  'No error in PR Input
GoTo 20

10  Workbooks.Open FileName:="C:\SPR\MPR Input.xlsm", _
    UpdateLinks:=3
Application.Run "'MPR Input.xlsm'!openInput"
Application.Run "'MPR Input.xlsm'!Finish"   'Saves & closes WB after running

    'Generate Exec pdf & xlsx reports
Workbooks.Open FileName:="C:\SPR\PR Executive Summary.xlsm"
Application.Run "'PR Executive Summary.xlsm'!Finish"
ActiveWorkbook.Save
ActiveWorkbook.Close

    'COOP closeout
Workbooks.Open FileName:="C:\SPR\COOP Data.xlsm", _
    UpdateLinks:=3
Application.Run "'COOP Data.xlsm'!EndMonth"
Application.Run "'COOP Data.xlsm'!EMcontd"  'Saves & closes WB after running

'COOP User files
Workbooks.Open FileName:="C:\School Payroll\COOP Report.xlsm", _
    UpdateLinks:=3
Application.Run "'COOP Report.xlsm'!UserRec" 'Saves/closes WB after running

'PS update
Workbooks.Open FileName:="C:\SPR\MPS.xlsm", _
    UpdateLinks:=3
Application.Run "'MPS.xlsm'!Finish"  'Saves & closes WB after running
Windows("Task.xlsm").Activate

'Clean PR input
Workbooks.Open FileName:="C:\SPR\MPR Input.xlsm", _
    UpdateLinks:=3
Application.Run "'MPR Input.xlsm'!openInput"
Application.Run "'MPR Input.xlsm'!CleanCells"
Windows("Task.xlsm").Activate

GoTo 100
20  MsgBox "You cannot closeout. You must Clear the  Error(s) in PR Input"

100 End Sub

2 个答案:

答案 0 :(得分:0)

第一次误读 - 让我再试一次。

它在哪里特别落下?

您发布的代码中的第一行是Sheets("Task").Select

可能很难找到工作表。

尝试使用ActiveWorkbook.Sheets("Task").Select或者,如果工作表仅在工作簿中,宏也在使用中“ThisWorkbook.Sheets(”Task“)

答案 1 :(得分:0)

抱歉,我从此没有回来。我已经解决了这个问题,因为早先将所有宏分布在不同的工作簿上只有一个工作簿(我称之为主WB)。因此,主宏将打开每个工作簿&执行适用于工作簿的应用程序。有了这个,我能够在自动模式下运行。最终结果如下:

Sub Create1TNoNm()     如果是Range(" AI5")。Value = 0则GoTo 400' 1T

Workbooks.Open Filename:="A:\School FMS\No Name Bills.xlsm", UpdateLinks:=3
Application.Run "'No Name Bills.xlsm'!ThisWorkbook.OBills"
Sheets("DashBoard").Visible = xlSheetVeryHidden
Sheets("Old").Select
Application.DisplayAlerts = False


If Range("AJ10").Value = 0 Then GoTo 70      'Nur


If Range("BJ4").Value = 0 Then GoTo 20
Application.Run "'No Name Bills.xlsm'!NoNm1TRec"


20  If Range("BJ5").Value = 0 Then GoTo 30
Windows("No Name Bills.xlsm").Activate
Application.Run "'No Name Bills.xlsm'!NoNm1TPrep"


30  If Range("BJ6").Value = 0 Then GoTo 40
Windows("No Name Bills.xlsm").Activate
Application.Run "'No Name Bills.xlsm'!NoNm1TKG"


40  If Range("BJ7").Value = 0 Then GoTo 50
Windows("No Name Bills.xlsm").Activate
Application.Run "'No Name Bills.xlsm'!NoNm1TN1"


50  If Range("BJ8").Value = 0 Then GoTo 60
Windows("No Name Bills.xlsm").Activate
Application.Run "'No Name Bills.xlsm'!NoNm1TN2"


60  If Range("BJ9").Value = 0 Then GoTo 70
Windows("No Name Bills.xlsm").Activate
Application.Run "'No Name Bills.xlsm'!NoNm1TN3"     'End Nur




250 Windows("No Name Bills.xlsm").Activate
Sheets("DashBoard").Visible = True
ActiveWorkbook.Save
ActiveWorkbook.Close
300  MsgBox "No name Bills of Old & New students for all classes successfully generated. Click on view/print button to view/print these bills. Click OK to continue"
GoTo 500
400  MsgBox "You do not have access to this due to your selection in the Setup Form. Click OK to continue"
Application.DisplayAlerts = True
500 End Sub         'End 1T