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.
Move or Copy both methods are underlying to Sheet object which offers two optional arguments as “After” and “Before” to determine the placement of sheet.
Prerequisites:
- Visual Studio 2015 or above having Microsoft Office for Developer Tool installed
- Create Excel Addin in C# code style (Visual Studio Tools for Office)
- Create a ribbon designer and put button
Code example:
private void btnMoveOrCopySheet_Click(object sender, RibbonControlEventArgs e)
{
//get reference of active workbook
excel.Workbook oWorkbook= Globals.ThisAddIn.Application.ActiveWorkbook;
//get reference of active worksheet
excel.Worksheet oWorksheet = Globals.ThisAddIn.Application.ActiveWorkbook.ActiveSheet;
//moving current sheet to second position in workbook by passing index as 1
oWorksheet.Move(After: oWorkbook.Worksheets[1]);
//make a copy of sheet and put before sheet 3 within workbook
oWorksheet.Copy(Before: oWorkbook.Worksheets[3]);
}
Next : Change sheet tab color using c#