Check columns are hidden in Excel Addin C#

Check columns are hidden in Excel Addin C#

Hidden Property:

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 column or columns, in this article I will try to cover a precheck which will ensure whether selected columns 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 btnHideColumns_Click(object sender, RibbonControlEventArgs e)
{
	//retain active row selection
	excel.Range oRange = Globals.ThisAddIn.Application.Selection;
	//check if selected columns are already in hidde mode?
	if (!oRange.Columns.Hidden)
		oRange.Columns.Hidden = true;
}

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

In above example data we will make selection between column “B” and column “C” and it should result as below:

Notice the output column “B” and “C” are hidden post execution of code. Developer can put a for loop to do the same.

Next : Dynamically hide/unhide sheets in Excel C#

Leave a Reply

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