How to: Programmatically Add New Worksheets to Workbooks

 

You can programmatically create a worksheet and then add the worksheet to the collection of worksheets in the workbook.

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

To add a new worksheet to a workbook in a document-level customization

  1. Use the Add method of the Sheets collection.

                Excel.Worksheet newWorksheet;
                newWorksheet = (Excel.Worksheet)Globals.ThisWorkbook.Worksheets.Add();
    

    The new worksheet is a native Microsoft.Office.Interop.Excel.Worksheet object and not a host item. If you want to add a Microsoft.Office.Tools.Excel.Worksheet host item, you should add the worksheet at design time.

To add a new worksheet to a workbook in a VSTO Add-in

  1. Use the Add method of the Sheets collection.

                Excel.Worksheet newWorksheet;
                newWorksheet = (Excel.Worksheet)this.Application.Worksheets.Add();
    

    The new worksheet is a native Microsoft.Office.Interop.Excel.Worksheet object and not a host item. You can also generate a Microsoft.Office.Tools.Excel.Worksheet host item from the native Microsoft.Office.Interop.Excel.Worksheet object. For more information, see Extending Word Documents and Excel Workbooks in VSTO Add-ins at Run Time.

Working with Worksheets
Host Items and Host Controls Overview
How to: Programmatically Delete Worksheets from Workbooks
How to: Programmatically Select Worksheets
Automating Excel by Using Extended Objects
Global Access to Objects in Office Projects
Optional Parameters in Office Solutions

Show: