工作簿不作为parameter passing在Excel中激活

我有一个Book1.xls Excel工作簿,它有一个macros写入,以便在工作簿打开macros运行。 这个macros将工作簿path中的所有CSV文件,并将所有的CSV合并成一张表,说Master.xlsx工作正常,并创buildMaster.xlsx。 在这个macros的末尾,我调用另一个写入同一工作表模块的macros,并将作为工作簿参数的Master.xlsx引用传递给另一个macros

现在我想要的是我需要设置Master.xlsx作为当前/活动工作簿传递参数给这个macros(模块),以便我可以格式化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 

相同工作簿的模块中的macros代码

 Sub AnotherMacroInModuleOfSameWorkbook(wb As Workbook) wb.Activate MsgBox (wb.Name) MsgBox (ActiveWorkbook.Name) End Sub 

在这里,我收到警报1的Master.xlsx ”, 警报2收到“ Book1.xls

我想要的是因为我从上面的macros传递Master.xlsx的引用,然后激活下面的macros中的Master.xlsx,警报2应该已经给“Master.xlsx”作为警报。

请帮忙。

谢谢。

通过改变这条线,Master工作performance在打开,以前不是。 它只是访问它。 我使用我自己的工作簿进行了testing,并将您的代码作为基础。 但是,我没有使用所有的代码,因为我没有这些对象。 所以大部分都是testing过的 在使用这行代码解决之前,我确实产生了相同的错误,所以我很肯定这解决了你的问题:

 Set wBM = Application.Workbooks.Open(mF) 

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

 Application.EnableCancelKey = xlDisabled 

警告:如果你这样做,你将无法打破你的代码,如果你产生一个无限循环。

请参阅关于如何处理EnableCancelKey的这篇文章

您还试图打开.xlsx文件,而不是.xlsm将其包含在您的文件创build语句中。

 FileFormat:= _xlOpenXMLWorkbookMacroEnabled 

我发现这个问题的解决方法。 我试图closures生成的主文件(wBM),并再次打开使用工作簿(mF).Open主工作簿,最终使我当前工作簿(主)作为活动工作簿。 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) 

感谢所有的答案。