通过作为参数传递,不在Excel中激活工作簿

时间:2014-12-18 10:18:37

标签: excel vba excel-vba

我有一个 Book1.xls Excel工作簿,其中写有一个宏,以便在工作簿打开宏运行。 此宏将获取工作簿路径中的所有CSV文件,并将所有CSV合并为单个工作表,如 Master.xlsx ,它可以正常工作并创建Master.xlsx。 在这个宏的末尾,我调用另一个宏写在同一个工作表的模块中,并将Master.xlsx引用作为工作簿参数传递给另一个宏

现在我想要的是我需要将Master.xlsx传递给此宏(模块)的参数作为当前/活动工作簿,以便我可以格式化master.xlsx的内容< / p>

Book1.xls的我的代码是:

Private Sub Workbook_Open()

    'Create Excel application instance
    Dim xlApp As Object
    Dim dt, masterpath, folderPath, fileName, dtFolder As String
    Set xlApp = CreateObject("Excel.Application")

    'Setup workbooks
    Dim wb As Excel.Workbook
    Dim wBM As Excel.Workbook
    Dim Wk As Workbook

    fileName = "C:\Master.xlsx"

    'Create a new Workbook
    Set Wk = Workbooks.Add
    Application.DisplayAlerts = False
    Wk.SaveAs fileName:=fileName
    Wk.Close SaveChanges:=False
    Application.DisplayAlerts = True

    'Csv files folder
    Dim CSVfolder As String
    CSVfolder = masterpath

    'Master Excel file path
    Dim mF As String
    mF = fileName 'Where your master file is

    'open the master file
    Set wBM = xlApp.Workbooks.Open(mF)

    'search and open the client files
    Dim fname As String
    fname = Dir(CSVfolder & "\*.csv")
    Do While fname <> ""
       'open the client file
       Set wb = xlApp.Workbooks.Open(CSVfolder & "\" & fname)
       'copy the first sheet from client file to master file
       wb.Sheets(1).Copy After:=wBM.Sheets(wBM.Sheets.count)
       'save master file
       wBM.Save
       'close client file
       wb.Close False
       'move to next client file
       fname = Dir()
    Loop

    xlApp.Visible = True
    Set xlApp = Nothing

   Call AnotherMacroInModuleOfSameWorkbook(wBM)

End Sub

同一工作簿模块中的宏代码

Sub AnotherMacroInModuleOfSameWorkbook(wb As Workbook)

wb.Activate
MsgBox (wb.Name)
MsgBox (ActiveWorkbook.Name)

End Sub

我来这里&#34; Master.xlsx &#34;对于提示1 和&#34; Book1.xls &#34; 警告2

我想要的是,因为我从上面的宏传递Master.xlsx的引用然后激活下面的宏中的Master.xlsx,警报2应该给出&#34; Master.xlsx&#34;警惕。

请帮忙。

感谢。

2 个答案:

答案 0 :(得分:1)

通过更改此行,主页将立即打开,而不是之前的位置。它只是访问它。我使用自己的工作簿进行了测试,并使用您的代码作为基础。但是,我没有使用你的所有代码,因为我没有那些对象。所以它主要经过测试。在使用此行解决之前,我确实产生了相同的错误,因此我非常肯定这可以解决您的问题:

Set wBM = Application.Workbooks.Open(mF)

问题在于,当您打开它时,代码将会中断并需要继续。要解决此问题,您需要在打开工作簿之前放置以下行。

Application.EnableCancelKey = xlDisabled

警告:如果这样做,如果生成无限循环,则无法破解代码。

Please see this article about how to deal with EnableCancelKey

您还尝试打开.xlsx文件,而不是.xlsm 将此文件包含在文件创建语句中。

FileFormat:= _xlOpenXMLWorkbookMacroEnabled

答案 1 :(得分:0)

我找到了解决此问题的方法。 我尝试关闭生成的主文件(wBM)并再次使用工作簿(mF)打开主工作簿。打开最终给我当前工作簿(主)作为活动工作簿。 Phewww .. !!!!困难时期

这是当前工作代码的快照:

Private Sub Workbook_Open()

    'Create Excel application instance
    Dim xlApp As Object
    Dim dt, masterpath, folderPath, fileName, dtFolder As String
    Set xlApp = CreateObject("Excel.Application")

    'Setup workbooks
    Dim wb As Excel.Workbook
    Dim wBM As Excel.Workbook
    Dim Wk As Workbook

    fileName = "C:\Master.xlsx"

    'Create a new Workbook
    Set Wk = Workbooks.Add
    Application.DisplayAlerts = False
    Wk.SaveAs fileName:=fileName
    Wk.Close SaveChanges:=False
    Application.DisplayAlerts = True

    'Csv files folder
    Dim CSVfolder As String
    CSVfolder = masterpath

    'Master Excel file path
    Dim mF As String
    mF = fileName 'Where your master file is

    'open the master file
    Set wBM = xlApp.Workbooks.Open(mF)

    'search and open the client files
    Dim fname As String
    fname = Dir(CSVfolder & "\*.csv")
    Do While fname <> ""
       'open the client file
       Set wb = xlApp.Workbooks.Open(CSVfolder & "\" & fname)
       'copy the first sheet from client file to master file
       wb.Sheets(1).Copy After:=wBM.Sheets(wBM.Sheets.count)
       'save master file
       wBM.Save
       'close client file
       wb.Close False
       'move to next client file
       fname = Dir()
    Loop

    'close the current workbook
    wBM.Close False
    xlApp.Visible = True
    Set xlApp = Nothing

    'setting the reference again
    Set newfile = Workbooks.Open(mF)        

    MsgBox (newfile.Name)
    MsgBox (ActiveWorkbook.Name)
   'Call to another module
   Call AnotherMacroInModuleOfSameWorkbook(wBM)

End Sub

这两行就是诀窍:

'close the current workbook
        wBM.Close False
'setting the reference again
        Set newfile = Workbooks.Open(mF)   

感谢所有答案。