我如何将多个工作表和多个图表obj表复制到另一个工作簿

时间:2015-04-07 09:46:38

标签: excel vba excel-vba

Sub cpySht2nWB()
Dim BkName As String
Dim NumSht As Integer
Dim BegSht As Integer

BegSht = 1

NumSht = ActiveWorkbook.Worksheets.Count
BkName = ActiveWorkbook.Name

Set NewBook = Workbooks.Add
With NewBook
    .SaveAs FileName:="C:\E2EPerformance\test.xlsm", FileFormat:=xlOpenXMLWorkbookMacroEnabled
End With

For x = 1 To NumSht

    Workbooks(BkName).Sheets(x).Move _
    Before:=Workbooks("test.xlsm").Worksheets(x)

Next

End Sub

程序错误:

  

运行时错误'1004':   Excel无法将工作表插入目标工作簿,因为它包含的行和列比源工作簿少。若要将数据移动或复制到目标工作簿,您可以选择数据,然后使用复制和粘贴命令插入另一个工作簿的工作表。

假设:我有10个工作表和10个图表对象表 我怎么能解决这个问题?

1 个答案:

答案 0 :(得分:0)

尝试改变结局: 如果您还有图表,工作表不会包含它们,请仅使用表格

Sub cpySht2nWB()
    Dim BkName As String
    Dim NumSht As Integer
    Dim BegSht As Integer
    Dim NewBook As Workbook

    BegSht = 1

    NumSht = ActiveWorkbook.Worksheets.Count
    BkName = ActiveWorkbook.Name

    Set NewBook = Workbooks.Add
    With NewBook
        .SaveAs FileName:="C:\E2EPerformance\test.xlsm", FileFormat:=xlOpenXMLWorkbookMacroEnabled
    End With

    For x = 1 To NumSht

        Workbooks(BkName).Sheets(x).Move _
        Before:=NewBook.Worksheets(NewBook.Worksheets.Count)

    Next x
End sub