Vba 工作簿未通过作为参数传递在Excel中激活

Vba 工作簿未通过作为参数传递在Excel中激活,vba,excel,Vba,Excel,我有一个Book1.xlsExcel工作簿,其中有一个宏,可以在工作簿打开时运行宏。 此宏获取工作簿路径中的所有CSV文件,并将所有CSV合并到单个工作表中,例如Master.xlsx,该工作表工作正常并创建Master.xlsx。 在这个宏的末尾,我调用了在同一工作表的模块中编写的另一个宏,并将Master.xlsx引用作为工作簿参数传递给另一个宏 现在我想要的是,我需要设置Master.xlsx将一个参数传递给这个宏(模块),作为当前/活动的工作簿,以便我可以格式化Master.xlsx的内

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

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

我的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
在这里,我为警报1获得了“Master.xlsx”,为警报2获得了“Book1.xls

我想要的是,因为我正在从上面的宏传递Master.xlsx的引用,然后在下面的宏中激活Master.xlsx,所以警报2应该给出“Master.xlsx”作为警报

请帮忙


谢谢。

通过更改这一行,母版纸现在打开了,以前没有打开过。它只是在访问它。我使用自己的工作簿进行了测试,并将您的代码作为基础。但是,我没有使用您的所有代码,因为我没有这些对象。所以它大部分是经过测试的。我确实产生了与您在使用这一行解决问题之前相同的错误,因此我非常确定这解决了您的问题:

Set wBM = Application.Workbooks.Open(mF)
问题是,当你打开它时,代码会中断,需要继续。要解决这个问题,您需要在打开工作簿之前放置以下行

Application.EnableCancelKey = xlDisabled
请注意:如果执行此操作,则如果生成无限循环,将无法中断代码

您还试图打开一个.xlsx文件,而不是.xlsm请在文件创建语句中包含该文件

FileFormat:= _xlOpenXMLWorkbookMacroEnabled

我找到了解决这个问题的方法。 我尝试关闭生成的主文件(wBM),然后再次使用工作簿(mF).open打开主工作簿,最终将当前工作簿(主工作簿)作为活动工作簿。 呸。。!!!!艰难时期

以下是当前工作代码的快照:

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

谢谢你的回答

一个细节:你可能想要
dimdt作为字符串,masterpath作为字符串。。。作为字符串
而不是
Dim dt,主路径。。。As String
@sancho.s:正如我所说的,第一个宏正在正常运行并生成Master.xlsx。所以你指出的上述语法错误是无效的。一切正常。您似乎误读了我的问题,我面临着新工作表的引用问题,该工作表是由第二个宏中的第一个宏生成的。i、 e我无法将作为工作簿对象从调用宏传递到被调用宏的Master.xlsx引用。谢谢。你的发现对我来说也很奇怪。要尝试的内容:在
wb处放置一个断点。激活
,然后运行通常的宏序列。执行中断后,执行
?在跨过接下来的三行之前,在即时窗口中显示wb.Name、ActiveWorkbook.Name
。您可能会在那里找到答案。请使用其他内容,而不是“wb作为工作簿”中的变量“wb”,并尝试使用“mwb作为工作簿”。@PareshJ:尝试过,但不起作用。这里的问题是工作簿未被激活。MsgBox(wb.Name)wb.Activate MsgBox(ActiveWorkbook.Name)。第一个警报显示“Master.xlsx”,第二个警报显示“Book1.xlsx”,即使在激活Master.xlsxApplication.enableCanceKey=xlEnabled之后,其中xlEnabled不是有效变量谢谢。修改为包含MSDN中关于处理这种情况的文档。抱歉,但不起作用。你能把你在我的场景中测试过的主文件放进去吗?主文件正在创建,在整个执行过程中也会打开,即使我没有改变你上面的建议。
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)