title | description | ms.date | ms.topic | dev_langs | helpviewer_keywords | author | ms.author | manager | ms.subservice | ||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Programmatically copy worksheets |
Copy a Microsoft Office Excel worksheet and insert the worksheet copy before or after an existing worksheet in the workbook programmatically from Visual Basic or C#. |
02/02/2017 |
how-to |
|
|
John-Hart |
johnhart |
mijacobs |
office-development |
You can create a copy of a worksheet, and insert that worksheet before or after an existing worksheet in the workbook. If you do not specify where to insert the worksheet, Excel creates a new workbook to contain the new worksheet.
[!INCLUDEappliesto_xlalldocapp]
Note
Whether you copy the worksheet programmatically, or the end user copies the worksheet manually, there is no code behind the new worksheet and controls on the new worksheet do not function. This is because the newly copied worksheet is a xref:Microsoft.Office.Interop.Excel.Worksheet object and not a xref:Microsoft.Office.Tools.Excel.Worksheet host item. Windows Forms controls and host controls can only be added to host items. For more information, see Programmatic limitations of host items and host controls.
-
Use the xref:Microsoft.Office.Interop.Excel.Worksheets.Copy%2A method to copy the first worksheet in the current workbook and place the copy after the third sheet.
:::code language="csharp" source="../vsto/codesnippet/CSharp/Trin_VstcoreExcelAutomationCS/Sheet1.cs" id="Snippet16":::
-
Use the xref:Microsoft.Office.Interop.Excel.Worksheets.Copy%2A method to copy the first worksheet in the current workbook and place the copy after the third sheet.
:::code language="csharp" source="../vsto/codesnippet/CSharp/trin_vstcoreexcelautomationaddin/ThisAddIn.cs" id="Snippet12":::
- Work with worksheets
- Host items and host controls overview
- How to: Programmatically add new worksheets to workbooks
- How to: Programmatically delete worksheets from workbooks
- How to: Programmatically select worksheets
- Automate Excel by using extended objects
- Global access to objects in Office projects
- Programmatic limitations of host items and host controls
- Optional parameters in Office solutions