复制隐藏的Excel工作表

时间:2019-05-23 16:18:23

标签: excel vba

我正在尝试复制一个隐藏的Excel工作表,但是显示错误 “工作表类的复制方法失败”

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

1 个答案:

答案 0 :(得分:3)

.Copy方法仅使用复制的工作表创建一个新的工作簿。任何工作簿中至少必须有一个工作表可见(您可以尝试通过仅创建一个工作表来创建一个新工作簿,然后尝试将其隐藏来独立地进行验证:

enter image description here

因此解决方案应该是在复制之前先取消隐藏,然后在复制后在源工作簿中隐藏

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
相关问题