我有这个子处理宏,我加入了2个excel文件,一切正常。
现在我已经知道要处理的文件不会有这些宏,我应该将它们存储在别处。我把它们放在一个空的工作簿中,我可以调用宏。
问题是,它的目标本身(一个空的工作簿)。 我需要处理我打开的Excel(x1)
原来是
xl.Run macroName
宏和目标工作表包含在这里
现在我有了这个,但我需要它在x1的工作表上运行,而不是自己。
myMacro.Run macroName
'runs but no worksheets here
这是sub
的完整代码Sub prepareReport(macroName As String)
Dim myMacro As Object
Dim xl As Object
Dim filePath As String
Dim fileName As String
fileName = determineFileNameAuto(macroName)
filePath = CurrentProject.Path + "\source_files\" + fileName
'load macro-excel files
Set myMacro = CreateObject("Excel.Application")
myMacro.Workbooks.Open (myMacroFilePath)
'Load Excel file, then open the target workbook.
Set xl = CreateObject("Excel.Application")
xl.Workbooks.Open (filePath)
'Run the macros
'originally was
'xl.Run macroName
'macros were contained within file
'this will run but the target should be the x1 workbook
myMacro.Run macroName
' Close and save the workbook, then close Excel
echoBackToForm ("Closing Workbook")
xl.ActiveWorkbook.Close (True)
xl.Quit
'Step 5: Memory Clean up.
Set xl = Nothing
End Sub