Delete Entire Row in Excel using VSTO C#

Delete Entire Row in Excel using VSTO C#

EntireRow:

It is a get method which returns Range object that represents the entire Row or Rows that contains the specified range. EntireRow has Delete() method which deletes entire row in referenced range. It has optional constants which can be specified to make sure the direction unlike Cells delete method which derive through “xlDeleteShiftDirection” enum having two constants:

  • xlShiftToLeft
  • xlShiftUp

Prerequisites:

  • Visual Studio 2015 or above having Microsoft Office for Developer Tool installed
  • Create Excel Addin in C# code style
  • Create a ribbon designer and put button

Code Example:

private void btnDeleteRow_Click(object sender, RibbonControlEventArgs e)
{
	//capture user selection
	excel.Range oRange = Globals.ThisAddIn.Application.Selection;
	//delete entire row
	oRange.EntireRow.Delete();
}

Let’s see before and after code execution, how data would look like in our Excel File:

Before:

Post code execution:

If you compare both screenshots the text in column “A” “I am Middle Aligned Text” the entire row is deleted and “I am Bottom Aligned Text” row moved up. Please leave your valuable comments!!!

Next : Delete entire column in Excel VSTO C# Addin

Leave a Reply

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