简体   繁体   中英

Copy a Hidden Excel Sheet

I am trying to copy a hidden excel worksheet, but it shows error "copy method of worksheet class failed"

Workbooks("FCD Alert").Activate
xPath = Application.ActiveWorkbook.Path

    With Application
        .ScreenUpdating = False
        .EnableEvents = False
        DisplayAlerts = False
    End With

    Set Sourcewb = ActiveWorkbook
    Sheets("Yesterday").Activate
    'Copy the ActiveSheet to a new workbook
    Sheets("Yesterday").Copy <- This region is getting Highlighted 
    Set Destwb = ActiveWorkbook

    'Determine the Excel version and file extension/format
    With Destwb
        If Val(Application.Version) < 12 Then
            'You use Excel 97-2003
            FileExtStr = ".xls": FileFormatNum = -4143
        Else

The .Copy method creates a new workbook with only the copied sheet. At least one sheet must be visible in the any workbook (you can verify this independently by trying to create a new workbook with only 1 worksheet, and then try to Hide it:

在此处输入图片说明

So the solution should be to unhide it before you copy and then hide it in the source workbook after it has been copied.

Set Sourcewb = ActiveWorkbook
Dim yesterday as Worksheet
Dim wsVis as Long
Set yesterday = Sourcewb.Sheets("Yesterday")
wsVis = yesterday.Visible  ' # Get the sheet's visible state
yesterday.Visible = xlSheetVisible  ' # Make it Explicitly visible
'Copy the ActiveSheet to a new workbook
Set Destwb = yesterday.Copy
yesterday.Visible = wsVis ' # return it to its original visible state

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