从另一个工作簿运行宏

时间:2015-12-10 20:51:39

标签: excel-vba vba excel

我在工作簿A中有一个宏调用工作簿B中的宏。我希望工作簿B中的宏运行,然后我想关闭工作簿B.我一直收到错误,说我找不到我想要的宏从工作簿B开始运行。我在这方面几乎是一个新手,但我做了一个非常彻底的搜索,并且无法自己想出任何东西。这是我的完整代码。

Public Sub InputDept()


Dim Cap As Workbook
Dim Cap2 As String

On Error Resume Next
Set Cap = Workbooks("NGD Source File for Net Budget Reporting.xlsx")
Cap2 = Cap.Name
On Error GoTo 0

Dim wb As Workbook
Dim Cap1 As Variant

Application.ScreenUpdating = False
If Cap Is Nothing Then
Cap1 = Application.GetOpenFilename("Excel Files(*.xl*)," & "*.xl*", 1)
    If Cap1 = False Then
    Exit Sub
    End If
Set wb = Workbooks.Open(Cap1)
Cap2 = ActiveWorkbook.Name
Else
Workbooks(Cap2).Activate
End If


Sheets("Dept Summary").Activate


Cells.Find(What:="Direct", after:=ActiveCell, LookIn:=xlFormulas, _
    LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
    MatchCase:=False, SearchFormat:=False).Offset(1, 0).Select

Range(Selection, Selection.End(xlDown)).Select

Dim cRng As Range
Dim dRng As Range

Set dRng = Selection

For Each cRng In dRng
If cRng.Interior.ThemeColor = xlThemeColorAccent3 Then


    Dim mCalc As String
    Dim mSum As Workbook

    On Error Resume Next
    Set mSum = Workbooks("Master Calc with Macro.xlsm")
    mCalc = mSum.Name
    On Error GoTo 0

    Application.ScreenUpdating = False
    If mSum Is Nothing Then
        mSum1 = Application.GetOpenFilename("Excel Files.xl*),"& "*.xl*", 1)
    If mSum1 = False Then
        Exit Sub
    End If
        Set wb1 = Workbooks.Open(mSum1)
        mCalc = ActiveWorkbook.Name
    Else
        Workbooks(mCalc).Activate
    End If

    cRng.Copy

    Workbooks(mCalc).Activate
    Sheets("Data").Select
    Range("A5").Select

    Selection.PasteSpecial Paste:=xlPasteValues
    Sheets("Report").Activate

    Workbooks(mCalc).Application.Run ("!SummarizeMaster")

    Sheets("Report").Select
    ActiveSheet.Copy
    Cells.Select
    Cells.Copy
    Selection.PasteSpecial Paste:=xlPasteValues
    ActiveWorkbook.SaveAs _
        Filename:=Application.ThisWorkbook.Path & "\" & Format(Date -        28, "MMM") & " Files\" & Left(cRng, 7) & ".xlsx"

    ActiveWorkbook.Close

    Workbooks(mCalc).Close savechanges:=False

End If
Next cRng



End Sub

3 个答案:

答案 0 :(得分:9)

这一行:

Workbooks(mCalc).Application.Run ("!SummarizeMaster")

需要改变一点。您需要在单引号中包含工作簿的名称,即使您使用Workbooks(mCalc)指定正确的工作簿:

Workbooks(mCalc).Application.Run ("'Master Calc with Macro.xlsm'!SummarizeMaster")

您实际上可以将其缩短为:

Application.Run ("'Master Calc with Macro.xlsm'!SummarizeMaster")

答案 1 :(得分:2)

如果宏需要通过使用运行宏的工作簿路径找到相对宏路径,并且需要从数组列表中运行多个宏,则下面的代码将有所帮助:

Dim relativePath As String, programFileName As String
Dim selectedProgramsFiles() As String, programsArrayLastIndex As Byte, I As Byte

For I = 0 To programsArrayLastIndex 'Loop through all selected programs
    programFileName = selectedProgramsFiles(I)
    relativePath = ThisWorkbook.Path & "\" & programFileName
    Workbooks.Open Filename:=relativePath

    Application.Run ("'" & relativePath & "'!ModuleName.Main")

   Workbooks(programFileName).Activate
   ActiveWorkbook.Close SaveChanges:=False
Next I 'For I = 0 To programsArrayLastIndex 'Loop through all selected program

答案 2 :(得分:1)

Application.Run "PERSONAL.xlsb!ClearYellow", 0

ClearYellowPersonal.xlsb中正在运行的子目录的名称。 “ 0”是此子项的第一个参数(如果没有参数,则将省略,可以添加更多用逗号分隔的参数)

Application似乎不是必需的

这也可以从其他一些工作簿中运行;该工作簿必须是开放的;如果该工作簿的名称中有空格,则该名称必须用''

包围

Call在跨工作簿中不起作用;没有在同一工作簿或同一模块中进行测试