Partilhar via


How to: Programmatically Delete Worksheets from Workbooks

You can delete any worksheet in a workbook. To delete a worksheet, use the worksheet host item or access the worksheet by using the sheets collection of the workbook.

Applies to: The information in this topic applies to document-level projects and application-level projects for Excel 2013 and Excel 2010. For more information, see Features Available by Office Application and Project Type.

Using the Worksheet Host Item

If the worksheet was added at design-time in a document-level customization, use the Delete method to delete a specified worksheet. The following code deletes a worksheet from a workbook by referencing the worksheet host item directly.

Important

This code runs only in projects that you create by using any of the following project templates:

  • Excel 2013 Workbook

  • Excel 2013 Template

  • Excel 2010 Workbook

  • Excel 2010 Template

If you want to perform this task in any other type of project, you must add a reference to the Microsoft.Office.Interop.Excel assembly, and then you must use classes from that assembly to open a workbook and delete a worksheet. For more information, see How to: Target Office Applications Through Primary Interop Assemblies and Excel 2010 Primary Interop Assembly Reference.

To delete a worksheet by using a worksheet host item

  • Call the Delete method of Sheet1.

    Globals.Sheet1.Delete()
    
    Globals.Sheet1.Delete();
    

Using the Sheets Collection of the Excel Workbook

Access worksheets through the Microsoft Office Excel Sheets collection in the following cases:

  • You want to delete a worksheet in an application-level add-in.

  • The worksheet that you want to delete was created at run time in a document-level customization.

The following code deletes a worksheet from a workbook by referencing the sheet through the index number of the Sheets collection. This code assumes that a new worksheet was created programmatically.

Important

This code runs only in projects that you create by using any of the following project templates:

  • Excel 2013 Workbook

  • Excel 2013 Template

  • Excel 2013 Add-in

  • Excel 2010 Workbook

  • Excel 2010 Template

  • Excel 2010 Add-in

If you want to perform this task in any other type of project, you must add a reference to the Microsoft.Office.Interop.Excel assembly, and then you must use classes from that assembly to open a workbook and delete a worksheet. For more information, see How to: Target Office Applications Through Primary Interop Assemblies and Excel 2010 Primary Interop Assembly Reference.

To delete a worksheet by using the Sheets collection of the Excel workbook

  • Call the Delete method of the Sheets collection.

    CType(Me.Application.ActiveWorkbook.Sheets(4), Excel.Worksheet).Delete()
    
    ((Excel.Worksheet)this.Application.ActiveWorkbook.Sheets[4]).Delete();
    

See Also

Tasks

How to: Programmatically Hide Worksheets

How to: Programmatically Move Worksheets Within Workbooks

How to: Programmatically Select Worksheets

How to: Programmatically Add New Worksheets to Workbooks

Concepts

Working with Worksheets

Worksheet Host Item

Global Access to Objects in Office Projects

Programmatic Limitations of Host Items and Host Controls