How to: 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 application-level projects for Excel 2007 and Excel 2010. 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
Use the Add method of the Sheets collection.
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 an application-level add-in
Use the Add method of the Sheets collection.
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 Application-Level Add-ins at Run Time.
Dim myExcel As Excel.Application
Private Sub btnNewSheet_Click(ByVal sender As System.Object, ByVal e As Microsoft.Office.Tools.Ribbon.RibbonControlEventArgs) Handles btnNewSheet.Click
Globals.ThisAddIn.Application.Worksheets.Add()
xlSheet1 = Globals.ThisAddIn.Application.Worksheets.Item(1)
CType(xlSheet1, Excel._Worksheet).Activate()
xlSheet1.Name = "new sheet"
End Sub
- 10/6/2011
- Muhammad Ali Mirza
- 11/20/2011
- Thomas Lee