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 Dynamic Filter

  • 2 min to read

This example demonstrates how to apply a dynamic filter to a column.

The dynamic filter allows you to apply one of the predefined date filters to display date values that fall within a specified time period (next, this or last week, month, year, etc.), or to display numbers that are above or below the average.

Note

The dynamic filter criteria can change when the data to which the filter is applied or the current system date changes.

  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 dynamic filter, call the AutoFilterColumn.ApplyDynamicFilter method, and pass the appropriate member of the DynamicFilterType enumeration as a parameter depending on the filter type you wish to apply.

    In this example, the two filter types are specified. The first filter criterion displays all the sales values in the third column that are above the average. The second filter criterion displays the reporting dates in the fourth column that occurred this year.

Dim worksheet As Worksheet = workbook.Worksheets("Regional sales")
workbook.Worksheets.ActiveWorksheet = worksheet

' Enable filtering for the specified cell range.
Dim range As Range = worksheet("B2:E23")
worksheet.AutoFilter.Apply(range)

' Apply a dynamic filter to the "Sales" column to display only values that are above the average.
worksheet.AutoFilter.Columns(2).ApplyDynamicFilter(DynamicFilterType.AboveAverage)
' Apply a dynamic filter to the "Reported Date" column to display values reported this year.
worksheet.AutoFilter.Columns(3).ApplyDynamicFilter(DynamicFilterType.ThisYear)