如何使用excel对象到vba中的另一个过程

我需要使用一个excel对象(在程序中设置),在同一个模块中的其他程序。 但不能做到这一点。 请帮助我做同样的事情。 以下是我的代码。

Public Sub FirstProc() 'Here the excel object is defined Set xlApp = CreateObject("Excel.Application") Set xlWorkbook = xlApp.Workbooks.Open(ReportTemplateDirectory\Test.xlsx") xlApp.Visible = True ... ... 'all work goes here FINE ... SecondProc 'calling second proc End Sub 

以下是我的第二个程序,

 Public Sub SecondProc() ' In this procedure i need to delete a sheet of the same excel file which is generated in above procedure. xlWorkbook.Sheets(4).Delete End Sub 

但是我得到了运行时错误424对象所需。 任何帮助,将不胜感激。

虽然我只是将该行添加到第一个模块,您可以尝试这样的事情(在Outlook中testing)。

closuresDisplayAlerts以避免在表单被删除时发出检查消息。

 Public Sub FirstProc() 'Here the excel object is defined Dim xlApp As Object Dim xlWorkbook As Object Set xlApp = CreateObject("Excel.Application") xlApp.displayalerts = False Set xlWorkbook = xlApp.Workbooks.Add xlApp.Visible = True Call SecondProc(xlWorkbook) 'calling second proc xlApp.displayalerts = True End Sub Public Sub SecondProc(xlWorkbook As Object) ' In this procedure i need to delete a sheet of the same excel file which is generated in above procedure. xlWorkbook.Sheets(2).Delete End Sub