在vba中复制所有活动或打开的工作簿中的特定工作表

时间:2017-12-11 06:04:39

标签: excel vba excel-vba

Sub moveotherschedulesheets()

Set wb1 = Workbooks("A.xlsx")
Dim wbs As Workbooks
Dim wb As Workbook
Set wbs = Application.Workbooks

For Each wb In wbs
    temp = wb.Name
Next
Workbooks("A.xlsx").Activate
With wb1
    Sheets(Array("A1", "A2", "A3", "A4", "A5", _
        "A6")).Select
    Sheets(Array("A1", "A2", "A3", "A4", "A5", _
        "A6")).Copy Before:=Workbooks(temp). _
        Worksheets(1)
End With

End Sub

在上述代码中,我正在尝试粘贴选定的工作表(" A1," A2"," A3"," A4&#34 ," A5" A5"," A6")在那个时间点的所有打开的工作簿中但是这段代码给了我错误的输出,因为它只复制了上次打开的工作簿中的选定工作表。请建议上述代码中所需的更改。

1 个答案:

答案 0 :(得分:0)

你应该移动

For Each wb In wbs
    temp = wb.Name
Next

之内
With wb1
    ...
End With

例如:

With wb1
    For Each wb In wbs
        Sheets(Array("A1", "A2", "A3", "A4", "A5", _
            "A6")).Copy Before:=wb.Worksheets(1)
        Next wb
End With

除此之外,您可能希望将工作簿A.xlsx排除为此复制操作的目标...