QP School

Full Version: Filter data in a worksheet
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Example:

Sub FilterData()
    Dim filterRange As Range

    'Set the range to be filtered (assuming data starts from A1)
    Set filterRange = Range("A1").CurrentRegion

    'Apply filter to column A for values greater than 5
    filterRange.AutoFilter Field:=1, Criteria1:=">5"
End Sub

Explanation:

The code defines a subroutine named FilterData.
It declares a variable filterRange, which represents the range to be filtered. 
In this example, the range is set to the data starting from cell A1 using the CurrentRegion property.
The filterRange.AutoFilter method is used to apply a filter to the range.
Field:=1 specifies that the filter should be applied to the first column of filterRange (column A).
Criteria1:=">5" sets the filtering criteria to show only values greater than 5.