Excel VBA - Worksheet类的复制方法失败

时间:2015-06-26 07:21:37

标签: excel vba excel-vba

我正在尝试从VBA复制工作表。这是我的代码:

'Create a new excel workbook and copy two result sheets.
Public Function copyAsSaveResultSheets(filePath As String)

    'Remove alert for confirmations
    Application.DisplayAlerts = False

    'Get new work book
    Dim newWorkBook As Workbook
    Set newWorkBook = Workbooks.Add

    'Copy result sheet
    'When click "Debug" cursor is here.
    ThisWorkbook.Sheets("ResultProcess").Copy Before:=newWorkBook.Sheets(1)
    ThisWorkbook.Sheets("ResultCode").Copy Before:=newWorkBook.Sheets(1)

    'Delete default sheet
    For Each sheet In Worksheets
        Select Case sheet.Name
            Case "Sheet1", "Sheet2", "Sheet3"
                sheet.Delete
        End Select
    Next

    'Save work book
    newWorkBook.SaveAs filePath

    'Close new work book
    ActiveWorkbook.Close

    'Available showing alert from application
    Application.DisplayAlerts = True

End Function

运行此代码时,出现此错误:

enter image description here

enter image description here

我的代码中没有发现任何错误。 “ResultProcess”表和“ResultCode”表根据要求受到保护。这有关系吗?

0 个答案:

没有答案