Re: Visual Studio Tools for Office Hiding Multiple Rows In Excel In One Command?
Crappy Name
Thanks. This was quicker, but still VERY slow. I found a way to do it minimizing my COM interop calls. Took a while to figure out, but check it out; it's instantaneous...
//
// function to filter rows that do not match criteria
//
private void HideRows3(Excel.Range in_range, string in_filterString, string in_filterString2, string in_filterString3)
{
string hideRef = null;
// convert to .net object to minimize com interop
Object[,] values = (Object[,])in_range.Value2;
// unhide all rows by default
in_range.EntireRow.Hidden = false;
// hide rows
bool hiding = false;
int startHide = 0;
for (int i = 1; i <= values.GetLength(0); i++)
{
// if we did not find match
if ((string)values[i, 1] != in_filterString || (string)values[i, 2] != in_filterString2 || (string)values[i, 3] != in_filterString3)
{
// if not currently hiding, set start
if (!hiding)
{
// keep start pos
startHide = i;
// now hiding
hiding = true;
}
}
else
{
// were we hiding
if (hiding)
{
// add to list
if (string.IsNullOrEmpty(hideRef))
hideRef = "A" + startHide.ToString() + ":A" + (i-1).ToString();
else
hideRef += ",A" + startHide.ToString() + ":A" + (i-1).ToString();
// no longer hiding
hiding = false;
}
}
}
// add last reference if we were hiding
if (hiding)
{
// add to list
if (string.IsNullOrEmpty(hideRef))
hideRef = "A" + startHide.ToString() + ":A" + values.GetLength(0).ToString();
else
hideRef += ",A" + startHide.ToString() + ":A" + values.GetLength(0).ToString();
}
try
{
// hide all rows in one command!
Excel.Range hideRange = (Excel.Range)in_range.get_Range(hideRef, System.Type.Missing);
hideRange.EntireRow.Hidden = true;
}
catch (Exception ex)
{
MessageBox.Show("Error " + ex.Message + " hiding rows");
}
}