How to copy sheets to another workbook using vba?

后端 未结 10 1714
故里飘歌
故里飘歌 2020-11-27 17:52

So, what I want to do, generally, is make a copy of a workbook. However, the source workbook is running my macros, and I want it to make an identical copy of itself, but wit

相关标签:
10条回答
  • 2020-11-27 17:59

    Try this instead.

    Dim ws As Worksheet
    For Each ws In ActiveWorkbook.Worksheets
        ws.Copy
    Next
    
    0 讨论(0)
  • 2020-11-27 18:02

    try this one

    Sub Get_Data_From_File()

         'Note: In the Regional Project that's coming up we learn how to import data from multiple Excel workbooks
        ' Also see BONUS sub procedure below (Bonus_Get_Data_From_File_InputBox()) that expands on this by inlcuding an input box
        Dim FileToOpen As Variant
        Dim OpenBook As Workbook
        Application.ScreenUpdating = False
        FileToOpen = Application.GetOpenFilename(Title:="Browse for your File & Import Range", FileFilter:="Excel Files (*.xls*),*xls*")
        If FileToOpen <> False Then
            Set OpenBook = Application.Workbooks.Open(FileToOpen)
             'copy data from A1 to E20 from first sheet
            OpenBook.Sheets(1).Range("A1:E20").Copy
            ThisWorkbook.Worksheets("SelectFile").Range("A10").PasteSpecial xlPasteValues
            OpenBook.Close False
            
        End If
        Application.ScreenUpdating = True
    End Sub
    

    or this one:

    Get_Data_From_File_InputBox()

    Dim FileToOpen As Variant
    Dim OpenBook As Workbook
    Dim ShName As String
    Dim Sh As Worksheet
    On Error GoTo Handle:
    
    FileToOpen = Application.GetOpenFilename(Title:="Browse for your File & Import Range", FileFilter:="Excel Files (*.xls*),*.xls*")
    Application.ScreenUpdating = False
    Application.DisplayAlerts = False
    
    If FileToOpen <> False Then
        Set OpenBook = Application.Workbooks.Open(FileToOpen)
        ShName = Application.InputBox("Enter the sheet name to copy", "Enter the sheet name to copy")
        For Each Sh In OpenBook.Worksheets
            If UCase(Sh.Name) Like "*" & UCase(ShName) & "*" Then
                ShName = Sh.Name
            End If
        Next Sh
    
        'copy data from the specified sheet to this workbook - updae range as you see fit
        OpenBook.Sheets(ShName).Range("A1:CF1100").Copy
        ThisWorkbook.ActiveSheet.Range("A10").PasteSpecial xlPasteValues
        OpenBook.Close False
    End If
    Application.ScreenUpdating = True
    Application.DisplayAlerts = True
    Exit Sub
    

    Handle: If Err.Number = 9 Then MsgBox "The sheet name does not exist. Please check spelling" Else MsgBox "An error has occurred." End If OpenBook.Close False Application.ScreenUpdating = True Application.DisplayAlerts = True End Sub

    both work as

    0 讨论(0)
  • 2020-11-27 18:06

    Someone over at Ozgrid answered a similar question. Basically, you just copy each sheet one at a time from Workbook1 to Workbook2.

    Sub CopyWorkbook()
    
        Dim currentSheet as Worksheet
        Dim sheetIndex as Integer
        sheetIndex = 1
    
        For Each currentSheet in Worksheets
    
            Windows("SOURCE WORKBOOK").Activate 
            currentSheet.Select
            currentSheet.Copy Before:=Workbooks("TARGET WORKBOOK").Sheets(sheetIndex) 
    
            sheetIndex = sheetIndex + 1
    
        Next currentSheet
    
    End Sub
    

    Disclaimer: I haven't tried this code out and instead just adopted the linked example to your problem. If nothing else, it should lead you towards your intended solution.

    0 讨论(0)
  • 2020-11-27 18:14

    Assuming all your macros are in modules, maybe this link will help. After copying the workbook, just iterate over each module and delete it

    0 讨论(0)
  • 2020-11-27 18:15

    I would like to slightly rewrite keytarhero's response:

    Sub CopyWorkbook()
    
    Dim sh as Worksheet,  wb as workbook
    
    Set wb = workbooks("Target workbook")
    For Each sh in workbooks("source workbook").Worksheets
       sh.Copy After:=wb.Sheets(wb.sheets.count) 
    Next sh
    
    End Sub
    

    Edit: You can also build an array of sheet names and copy that at once.

    Workbooks("source workbook").Worksheets(Array("sheet1","sheet2")).Copy _
             After:=wb.Sheets(wb.sheets.count)
    

    Note: copying a sheet from an XLS? to an XLS will result into an error. The opposite works fine (XLS to XLSX)

    0 讨论(0)
  • 2020-11-27 18:15
        Workbooks.Open Filename:="Path(Ex: C:\Reports\ClientWiseReport.xls)"ReadOnly:=True
    
    
        For Each Sheet In ActiveWorkbook.Sheets
    
            Sheet.Copy After:=ThisWorkbook.Sheets(1)
    
        Next Sheet
    
    0 讨论(0)
提交回复
热议问题