将Excel工作簿合并到工作表中

时间:2014-12-02 10:44:45

标签: excel vba merge

我目前正在尝试将24个工作簿合并为一个包含24张的工作簿。工作簿被命名为运行1运行24并且我正在尝试合并到一个模板中,该模板已经有两个名为summary和pressure的工作表。我很擅长编码,其他问题的任何复制代码似乎都不适合我。我尝试了移动到模板中的记录宏,但是当尝试应用它时,它会出现运行时错误9.编码看起来像这样。

Sub Macro1()
'
' Macro1 Macro
'

'
    Sheets("Sheet1").Select
    Sheets("Sheet1").Copy After:=Workbooks("Current Template.xlsx").Sheets(2)
    Sheets("Sheet1").Select
    Sheets("Sheet1").Move After:=Workbooks("Current Template.xlsx").Sheets(3)
End Sub

非常感谢任何帮助。

干杯

1 个答案:

答案 0 :(得分:0)

这可以帮到你:

Sub test()

    Dim wb As Workbook
    Set wb = Application.Workbooks("target.xlsm")  'Considering that macro is placed in your target file
    i = 1
    While i < 25
        Workbooks.Open ("run" & i & ".xlsx")   'give path as applicable
        Set wb1 = Application.Workbooks("run" & i & ".xlsx")
        With wb1
            .Sheets("sheet1").Copy After:=wb.Sheets(wb.Sheets.Count)   'the new sheet will be placed after the last sheet in target file
            .Close
        End With
    i = i + 1
    Wend

End Sub