The requested page is not available for the requested platform. You are viewing the content for .NET Framework 4.5.2+ platform.

How to: Apply a Custom Number Filter

  • 2 min to read

This example demonstrates how to specify the custom compound filter criteria to filter numbers in a column.

  1. Turn on the filtering functionality for the required range, as described in the How to: Enable Filtering example.
  2. Use the AutoFilterBase.Columns property of the SheetAutoFilter object to get a collection of columns in the filtered range (the AutoFilterColumnCollection object). Each column in the collection is defined by the AutoFilterColumn object, which provides basic methods for data filtering. To filter data in a particular column, get access to this column by its index in the AutoFilterColumnCollection collection.
  3. To apply a custom complex filter, call the AutoFilterColumn.ApplyCustomFilter method overload with five parameters.

    • Specify the first filter criterion. Pass 5000 as the filter criterion value, and use the FilterComparisonOperator.GreaterThanOrEqual member of the FilterComparisonOperator enumeration as the comparison operator to find sales values that are greater than or equal to 5000$.
    • Specify the second filter criterion. Further restrict your data by displaying only values that are less than or equal to 8000$. To do this, set the second criterion value to 8000, and select the FilterComparisonOperator.LessThanOrEqual comparison operator.
    • Pass the true value as the last parameter of the method to indicate that the AND operator should be used to combine the filter criteria specified above.
Worksheet worksheet = workbook.Worksheets["Regional sales"];
workbook.Worksheets.ActiveWorksheet = worksheet;

// Enable filtering for the specified cell range.
CellRange range = worksheet["B2:E23"];
worksheet.AutoFilter.Apply(range);

// Filter values in the "Sales" column that are in a range from 5000$ to 8000$.
AutoFilterColumn sales = worksheet.AutoFilter.Columns[2];
sales.ApplyCustomFilter(5000, FilterComparisonOperator.GreaterThanOrEqual, 8000, FilterComparisonOperator.LessThanOrEqual, true);