我有一个宏" PrepareSheets_and_FillData"在此文件中:" C:\ 2013-2014 \ C1 \ C1 Macro.xlsm"。
我需要在标有" 1NB"," 1SB"," 2NB"," 2SB"的文件上运行此宏。然后我需要将文件另存为" 1NB_Results"," 1SB_Results"所以代码需要打开一个文件,从另一个位置运行一个宏,然后根据约定保存文件。
有没有办法自动完成?
这是我使用的简单解决方案。
Application.Workbooks.Open ("C\2013-2014\C1\1WB.xlsx")
Sheets("Data").Select
Range("A2").Select
Application.Run "'C1 Macro.xlsm'!PrepareSheets_and_FillData"
Range("C16").Select
Application.CutCopyMode = False
ActiveWorkbook.SaveAs Filename:= _
"C:\2013-2014\C1\1WB Results.xlsx", FileFormat:= _
xlOpenXMLWorkbook, CreateBackup:=False
ActiveWindow.Close
然后重复每个文件。
答案 0 :(得分:1)
file_names = Array("1NB", "1SB", "2NB", "2SB")
For Each indiv_file In file_names
Application.Workbooks.Open ("C\2013-2014\C1\" & indiv_file & ".xlsx")
Sheets("Data").Select
Range("A2").Select
Application.Run "'C1 Macro.xlsm'!PrepareSheets_and_FillData"
Range("C16").Select
Application.CutCopyMode = False
ActiveWorkbook.SaveAs Filename:= _
"C:\2013-2014\C1\" & indiv_file & " Results.xlsx", FileFormat:= _
xlOpenXMLWorkbook, CreateBackup:=False
ActiveWindow.Close
Next
代码未经测试,可能存在语法错误
答案 1 :(得分:-1)
主宏怎么样,然后Call
执行魔法的子并迭代你需要处理的文件。