Page tree

Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Migration of unmigrated content due to installation of a new plugin
Wiki Markup
{description}
{excerpt}Copies a worksheet in the workbook to another position in the same workbook.{excerpt}
{signature:C#}
 public void CopySheet(Worksheet sheet, int position, System.String newName)
{signature}{signature:vb.net}
Public Sub CopySheet(ByVal sheet As Worksheet, ByVal position As Integer, ByVal newName As String)
{signature}
{parameters}
{param:sheet}A [Worksheet|Worksheet] object representing the sheet to copy.{param}
{param:position}The 0\-based position at which to insert a copy of the specified worksheet. If {{position}} is less than 0, it is inserted before the first sheet in the workbook. If {{position}} is equal to or greater than [Worksheets.Count|Worksheets.Count], it is inserted after the last worksheet.{param}
{param:newName}The name to use for the new worksheet.{param}
{remarks}The CopySheet method is intended to allow for copying of sheets from one position to another within a single workbook. To copy content from a worksheet in one workbook to worksheet in another workbook, use the [Worksheet.CopyPaste|Worksheet.CopyPaste(String, Area, CopyPasteProperties)]

All of the settings, configuration, and values on the original worksheet will be both preserved on the original worksheet and copied to the new worksheet. If there are any formulas in the worksheet that is copied that reference that worksheet, they will be updated to reference the new worksheet instead. Formulas on the original worksheet and on other worksheets will not be affected.
{introducedin: 8.2}
Copysheet was extended to support copying worksheets between workbooks for XLSX and XLSM files.
{remarks}



{example}{code:csharp|title=C#}
//An example of copying worksheets between workbooks
ExcelApplication xla = new ExcelApplication();
Workbook wbSource = xla.Open("sourceWorkbook");
Workbook wbDest = xla.Open("destWorkbook");

//Copy a worksheet from the source workbook into the destination workbook
//Insert the copy at the beginning of the workbook
Worksheet wsToCopy = wbSource.Worksheets[0];
wbDest.Worksheets.CopySheet(wsToCopy, 0, wsToCopy.Name);
{code}
{code:vbnet|title=vb.net}
'An example of copying worksheets between workbooks
Dim xla As ExcelApplication = new ExcelApplication()
Dim wbSource As Workbook = xla.Open("sourceWorkbook")
Dim wbDest As Workbook = xla.Open("destWorkbook")

'Copy a worksheet from the source workbook into the destination workbook
'Insert the copy at the beginning of the workbook
Dim wsToCopy As Worksheet = wbSource.Worksheets(0)
wbDest.Worksheets.CopySheet(wsToCopy, 0, wsToCopy.Name)
{code}

{example}