Rename Sheet in Excel Addin

Rename Sheet in Excel Addin

Rename:

There is Name property exposed by worksheet object which offers getter and setter methods by which we can retain or set sheet name. Let’s take simple example to understand how we can accomplish it.

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 btnRenameSheet_Click(object sender, RibbonControlEventArgs e)
{
	//get reference of active workbook
	excel.Worksheet oWorksheet = Globals.ThisAddIn.Application.ActiveWorkbook.ActiveSheet;
	//Before renaming sheet
	MessageBox.Show("Current sheet name is " + oWorksheet.Name);
	//rename sheet
	oWorksheet.Name = "I am renamed";
	//post sheet rename
	MessageBox.Show("After rename sheet name is " + oWorksheet.Name);
}

Execute the code by pressing CTRL + F5 and you observe following on rename sheet button click:

now click on OK on the Popup Box and another popup will appear having new sheet name as “I am renamed” and sheet4 would rename as shown below:

Next : Move or Copy sheet in Excel using C#

Leave a Reply

Your email address will not be published. Required fields are marked *