How to copy sheets to another workbook using vba?

后端 未结 10 1896
故里飘歌
故里飘歌 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 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.

提交回复
热议问题