×

Excel VBA AutoFilter

One of the reasons for Excel VBA’s popularity is its capability to filter and analyze data from huge database with the help of a method known as AutoFilter. This method permits a user to filter the particular data according to a set criterion and aids its users to draw out precise reports and research work on data. AutoFilter method can filter data comprising values with text, numbers, Boolean, or date data types. Whenever the user applies the AutoFilters to its Excel sheet worksheet, black drop-down arrows (also called filter switches) pop up at the right of your column headings.

Although, we have a filter option available under the Data ribbon tab. But this option is only useful to deal with a minimal set of data. For bulk, it becomes complex to use this method. Hence, to resolve this problem, VBA AutoFilter was introduced. AutoFiltering technique is faster and more efficient. Before applying AutoFilter in VBA, the user should always keep in mind to turn off any current filters (if present) and remove them completely. The range object can also be used to declare AutoFilter in Excel VBA.

Syntax

AutoFilter (Field, Criteria1, Operator, Criteria2, SubField, VisibleDropDown)

Parameter

Field (optional) - This parameter represents an integer offset of the field on which bases you want to apply the filter. The leftmost field is considered as field one.

Criteria1 (optional) – It specifies the criteria. For e.g., to specify blank cells use “=”, for non-blank cells use “<>” and for no data “><” is used.

Operator (optional) – It Identifies the operator to use to associate two criteria applied by a filter. It can accept the following values:

  • xlAnd – Logical ‘AND’ will be applied criterion 1 and criteria 2.
  • xlBottom10Items- The lowest value displayed in Criteria1.
  • xlBottom10Percent- The lowest-valued percentage displayed in Criteria1.
  • xlFilterCellColor- The cell color
  • xlFilterDynamic – The dynamic filter
  • xlFilterFontColor – The font color
  • xlFilterIcon – filter icon
  • xlFilterValues – filter values
  • xlOr - Logical ‘OR’ will be applied criterion 1 and criteria 2.
  • xlTop10Items - The highest value displayed in Criteria1.
  • xlTop10Percent - The highest-values percentage displayed in Criteria1.

Criteria2 (optional) - It specifies the criteria. For e.g., to specify blank cells use “=”, for non-blank cells use “<>” and for no data “><” is used.

SubField (optional)- This parameter represents the field from a data type on which the user wishes to apply the criteria. If this parameter is not specified, by default, it targets the "Display Value".

VisibleDropDown (optional) – This parameter accepts a Boolean value. If it is set to Boolean True, it displays the drop-down window for filtering the field else for Boolean False it will hide the AutoFilter drop-down arrow. Bu default, it is set to Boolean True.

Example

We will use the use Excel Sheet data and with the help of AutoFilter method will put on a criterion to fetch only those rows where sales filed is greater than 35.

Excel VBA AutoFilter

Code:

Sub AutoFilter_Example()
 With ActiveSheet
     'FilterMode Property determines whether the AutoFilters are filtering data down
     'and FilterMode are in use
     .AutoFilterMode = False
     'specifying the AutoFilter range
     .Range("A2:I21").AutoFilter
     'setting the parameters and applying criteria
     'at filed 5, filtering sales data more than 35 
     .Range("A2:I21").AutoFilter Field:=5, Criteria1:=35
 End With
 End Sub 

Let’s work with the set-by-step code of lines:

Step 1: Open the VBA developer tab either by using the shortcut keywords Alt +F11 or click on developer window -> visual basic editor.

Step 2: Visual Basic Editor will open. Next step is to create a module. Right-clicking on the VBA Project-> Click on Insert-> Click on Module.

Excel VBA AutoFilter

Step 3: In the VBA Module window, within the sub-block, introduce your macro name.

Excel VBA AutoFilter

Step 4: Next, we will use the ‘With’ block we will define a series of ActiveSheet.

Excel VBA AutoFilter

Step 5: Firstly, we will disable the AutoFilterMode to turn off any current filters (if present) and remove them completely. This property determines whether the AutoFilters are filtering data, or the AutoFilter arrows are visible or not. Although this is an optional method. Nut its always advisable to make sure that the previous filters are put off.

Excel VBA AutoFilter

Step 6:  Now, with the help of Range object we will select the data from our Excel active sheet wherein we wish to apply the auto filter method.

Excel VBA AutoFilter

Step 7: We will us the parameters of the AutoFilter method and will define the filed number and criteria to put the filter. Unlike in the below example, we have put field=5 and the sales criteria should be greater than 35.

Excel VBA AutoFilter

Output

Step 8: Execute the above code either by pressing the F5 shortcut key or by clicking on the Run button.

Step 9: You will notice that your sheet has been filtered and only the row whole sales filed is greater than is displayed. Thus, hiding all the other values. You can copy these values and can paste it at another sheet for your future reference.

Excel VBA AutoFilter

Related Topics

Excel VBA Mid Function

VBA Mid Function: The Mid function in VBA returns a substring from within a supplied string. Syntax Mid (Str, Start, [Length]) Parameter Str (required) -This parameter represents a string from which you want to extract the substring. Start...

1 minute read.

Excel VBA Oct Function

VBA Oct Function: The Oct function in VBA converts the given number into octal notation and returns the result as a string. Syntax Oct (Number) Parameter Number (required) – This parameter represents the numeric value...

1 minute read.

Excel VBA DatePart Function

The DatePart function in VBA returns a part (day, month, week, etc.) for the specified date and/or time. Syntax DatePart (Interval, Date, [FirstDayOfWeek], [FirstWeekOfYear]) Parameter Interval (required) – This parameter represents a string specifying the interval to be used. It can...

2 minutes read.

Procedures in VBA

Procedures in VBA A procedure is a block of statements or units of computer code that performs some action. It is enclosed with a declaration statement, and its primary purpose is to carry out...

3 minutes read.

Excel VBA CVErr Function

VBA CVErr Function: The CVErr function in VBA returns an Error data type, involving with a user-specified error code. Syntax CVErr (Expression) Parameter Expression (required)- This parameter represents the required error code. Return This function returns an...

1 minute read.

Excel VBA Error Function

VBA Error Function: The Error function in VBA returns the error message corresponding to a supplied error code. Syntax Error ([ErrorNumber]) Parameter ErrorNumber (optional) – This parameter represents the required error number. By default, the...

1 minute read.

Excel VBA ABS Function

VBA ABS Function: The ABS function in VBA returns the absolute value of the specified number. Syntax Abs (Number) Parameter Number (required) – This parameter represents the number that you want the absolute value of. Return This...

1 minute read.

Excel VBA While wend Loop

WHILE wend loop is used when the user is not sure how many times they want to execute the VBA code within the program. With a WHILE loop, the loop body may...

3 minutes read.

Excel VBA Sqr Function

VBA Sqr Function: The Sqr function in VBA returns the square root for the specified number. Syntax Sqr (Number) Parameter Number (required) – This parameter represents a positive numeric value that you want to calculate...

1 minute read.

Excel VBA Round Function

VBA Round Function: The Round function in VBA rounds a number to a specified number of decimal places and returns the number. Syntax Round (Number, [NumDigitsAfterDecimal]) Parameter Number (required) –This parameter represents a numeric value one wants...

1 minute read.

Excel VBA Choose Function

VBA Choose Function: The Choose function in VBA chooses function selects the corresponding value from a list of arguments depending as per the specified index. Syntax Choose (Index, [Choice-1], [Choice-2], ...) Parameter Index (required) – This...

2 minutes read.

Excel VBA Split Function

Excel VBA Split Function: The Split function in VBA is used to split a string into several substrings and return a one-dimensional array of substrings. Syntax Split (Expression, [Delimiter], [Limit], [Compare]) Parameter Expression (required) – This parameter...

2 minutes read.

Excel VBA DateAdd Function

The DateAdd function in VBA adds a time interval to a supplied date and/or time and returns the resultant date/time. Syntax Dateadd (Interval, Number, Date) Parameter Interval (required) – This parameter a string specifying the interval to be...

1 minute read.

VBA Object Required

What is Object Required Error? VBA Object Required is a run time error which occurs when the user does not define a valid object qualifier, or the assigned object doesn’t exist in the...

6 minutes read.

Excel VBA DateSerial Function

The DateSerial function in VBA returns a Date from a supplied year, month, and day number. Syntax DateSerial (Year, Month, Day) Parameter Year (required) – This parameter represents an integer signifying the year. Month (required) – This parameter...

2 minutes read.

Excel VBA Exp Function

VBA Exp Function: The Exp function in VBA returns the value of the exponential function ex (mathematical constant ‘e’ raised to specified power) for the given value of x. Syntax Exp (Number) Parameter Number (required) –This parameter represents...

1 minute read.

VBA ActiveSheet

What is VBA Activesheet Property? The active sheet means the current worksheet which you are working on and viewing. The ActiveSheet object signifies the worksheet tab that is selected before running the VBA...

5 minutes read.

VBA Color Index Property

What is Color Index Property? The Excel VBA Color Index is used to change the color for the cell or range of cells or text (located under the Font section). It sets the color...

5 minutes read.

Excel VBA Len Function

VBA Len Function: The Len function in VBA returns the number of characters in a supplied string or the number of bytes required to store a supplied variable. Syntax Len (Expression) Parameter Expression (required)-...

1 minute read.

Excel VBA Join Function

Excel VBA Join Function: The Join function in VBA is used to join an array of substrings and return them all as a single string. Syntax Join (SourceArray, [Delimiter]) Parameter SourceArray (required) – This parameter...

1 minute read.