如何将多个工作表从一个工作簿复制到另一个工作簿;没有复制VBA

时间:2015-03-02 13:09:01

标签: excel-vba vba excel

我正在尝试将所有工作表(7张)从workbook1(wb1)复制到wb2。 wb1包含命令按钮但我不想在我的新工作簿中使用它们。我正在使用循环将单个工作表从一个工作簿复制到另一个工作簿。但复制到第二张纸时出现错误。我使用的代码如下: -

 Public Sub CommandButton1_Click()
    Dim wb1 As Workbook
    Dim wb2 As Workbook
    Dim i As Integer
    i = 1
    Set wb1 = ActiveWorkbook
    Set ws1 = ActiveSheet
    Set wb2 = Workbooks.Add
    With wb2
    .Title = "My Sales"
    .Subject = "Sales"
    .SaveAs Filename:="mysales.xls"
    End With
    For i = 1 To 7
    Dim row As Long
    Dim column As Long
    wb1.Activate
    wb1.Sheets(i).Activate
      column = ActiveSheet.Cells(1, Columns.Count).End(xlToLeft).column
      row = ActiveSheet.Range("A" & Rows.Count).End(xlUp).row
     Application.CutCopyMode = False
    ActiveSheet.Range(Cells(1, 1), Cells(row, column)).Select
     Selection.Copy
      wb2.Sheets(i).Range("A1").PasteSpecial
     Application.CutCopyMode = False
    next i
End Sub

Loop第一次成功运行但是对于i = 2,代码给出了错误

1 个答案:

答案 0 :(得分:0)

Sub M_snb()
  thisworkbook.sheets(array("one","two","three","four","five","six","seven")).copy

  do until activeworkbook.sheets("one").oleobjects.count=0
   activeworkbook.sheets("one").oleobjects(1).delete
  loop
  activeworkbook.saveas "G:\OF\new.xlsx",51
End Sub