2015-03-02 311 views
0

我想從工作簿1(wb1)複製所有工作表(7張)到wb2。 wb1包含命令按鈕,但我不希望他們在我的新工作簿中。我正在使用循環將單個工作表從一個工作簿複製到另一個工作簿。但複製到第二張時出現錯誤。我使用的代碼如下: -如何將多張工作表從一個工作簿複製到另一個工作簿;沒有複製VBA

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 

循環是首次成功地運行,但對於i = 2,代碼提供了錯誤

回答

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 
+0

你能否解釋一下? – dgilperez 2015-03-03 00:32:08

相關問題