Check if rows are hidden in Excel Addin C#

Check if rows are hidden in Excel Addin C#

Hidden:

VSTO(Visual Studio Tools for Office) offers a versatile COM development environment which is capable to manipulate any properties, objects of Office applications. In Excel you might want to toggle hidden property of a row or rows, in this article I will try to cover a precheck which will ensure whether selected rows are hidden or not if not so it would hide the same.

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 btnHideRows_Click(object sender, RibbonControlEventArgs e)
{
	//retain active row selection
	excel.Range oRange = Globals.ThisAddIn.Application.Selection;
	//Check if rows are already hidden
	if (!oRange.Rows.Hidden)
		oRange.Rows.Hidden = true;
}

Above code will retain selection and perform a check if selected range has already hidden rows, if not then If block gets execute and it will mark rows as hidden as shown in below example:

Above screen I will make selection for row 3rd and 4th and execute the code and output should hide selected rows as shown below:

Hope you like this article, please feel free to leave your valuable comments!!!

Next : Hide/Unhide multiple columns in Excel C#

Leave a Reply

Your email address will not be published.