Skip to main content

How to: Filter by Cell Values

  • 2 minutes to read

This example demonstrates how to filter data in a column by a list of values.

  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. Call the AutoFilterColumn.ApplyFilterCriteria method and pass the FilterValue object as a parameter. This object defines the value(s) that should be used in the filter criterion. You can use the numeric, boolean, string, DateTime or CellValue types as a filter value. The passed value will be implicitly converted to the FilterValue object. In this example, an array of CellValue objects is used to display the specific items in the "Product" column.

For an example on how to filter data by a list of date values, refer to the How to: Filter by Date Values article.

View Example

Worksheet worksheet = workbook.Worksheets["Regional sales"];
workbook.Worksheets.ActiveWorksheet = worksheet;

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

// Filter the data in the "Product" column by an array of values.
worksheet.AutoFilter.Columns[1].ApplyFilterCriteria(new CellValue[] { "Mozzarella di Giovanni", "Gorgonzola Telino" });