Skip to main content

How to: Apply a Custom Date Filter

  • 3 minutes to read

This example demonstrates how to specify the custom filter criteria to display dates that are before, after or equal to the specified date, or between two dates.

  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 and pass the following parameters.

View Example

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 "Reported Date" column to display dates that are between June 1, 2014 and February 1, 2015.
worksheet.AutoFilter.Columns[3].ApplyCustomFilter(new DateTime(2014, 6, 1), FilterComparisonOperator.GreaterThanOrEqual, new DateTime(2015, 2, 1), FilterComparisonOperator.LessThanOrEqual, true);