简体   繁体   中英

Copy the sheet with index to a new Workbook

I use this code to copy sheets(i) to new workbook but it always error at ActiveWorkbook.Sheets(i).Copy . If I use specific sheet (like sheets("Handover") ) code run. Please help me explain what 's wrong

Sub CopyToNew()
    'Copy the sheets(i) to a new Workbook.
    For i = 3 To ActiveWorkbook.Sheets.Count
        ActiveWorkbook.Sheets(i).Copy
        Next
    End Sub

Assuming you have 3 or more worksheets, what's happening is that the first time you do ActiveWorkbook.Sheets(i).Copy , the newly created workbook becomes the ActiveWorkbook , with only one worksheet.

To avoid this anomaly, replace ActiveWorkbook with ThisWorkbook in your code.

Also, Check if there are hidden sheets in the workbook, the method Copy fails on hidden worksheets.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM