Advanced filters are very powerful and useful while dealing or filtering the data with verity of conditions. We can apply Advanced Filters using VBA. We can copy the data into another location or we ca filter the data in the same location.
Excel VBA Range Advanced Filter – Syntax
Here is the syntax or VBA code to apply the advanced filter in a range of data.
Range(“YourRange”).AdvancedFilter(Action As XlFilterAction,[CriteriaRange], [CopyToRange], [Unique])
- Here AdvancedFilter is a Member of Excel.Range Object.
- XlFilterAction: You can specify how you want perform the filter:
- Const xlFilterInPlace = 1 will filter in the range data source.
- Const xlFilterCopy = 2 will filter the data and copy to the target range, in this case you have to provide the [CopyToRange] which you want to copy the filtered data.
- [CriteriaRange]: You can specify the filter criteria range where you can mention any criteria including formulas.
- [Unique]: You can set to TRUE if you want to get unique records, False to set all records.
Excel VBA Range Advanced Filter- Examples
Here is the simple example to apply the advanced filer in a range. Assuming that you have data in Range “A3:B8” and You have specifies the criteria at Range “B1:B2”. Here we are using xlFilterInPlace to filter the data in the data range itself. and no need to provide the target range.Sub VBA_Range_Advanced_Filter() Range("A3:B8").AdvancedFilter Action:=xlFilterInPlace, _ CriteriaRange:=Range("B1:B2"), Unique:=False End Sub
Excel VBA Range Advanced Filter- Instructions
Please follow the below step by step instructions to execute the above mentioned VBA macros or codes:
- Open an Excel Workbook from your start menu or type Excel in your run command
- Now you have to prepare some sample data to test this macro.
Enter the header as SerialNumber at A3 and Value at B3)
- Enter some sample data from range A4 to B8, say 1 to 5 in SerialNumber and 100 to 500 in values fileld
- Now enter the criteria, I am entering at ‘SerialNumber’ at Range B1 and ‘>2’ as criteria at Range B2. So your criteria range will be “B1:B2”
- Press Alt+F11 to Open VBA Editor or you can goto Developer Table from Excel Ribbon and click on the Visual Basic Command to launch the VBA Editor
- Insert a Module from Insert Menu of VBA
- Copy the above code (for filtering the data in a range using Excel VBA) and Paste in the code window(VBA Editor)
- Save the file as Macro Enabled Workbook (i.e; .xlsm file format)
- Press ‘F5′ to run it or Keep Pressing ‘F8′ to debug the code line by line.
Now, You can observe that the records are displaying only for values 3,4 and 5.
Real-time Applications on Excel VBA Range Advanced Filters
Hide Developer Tab in Excel Ribbon using from the Excel options. It helps to prevent users from opening the Excel VBA editor window. Hide Developer Tab in Excel Ribbon: Right click on any main menu
Show or hide means to display or hide a Userform. We use Show and Hide methods to display or hide. Unload will be used when completes the task.
Column Number to Column Name is nothing but converting column number to Excel alphabetic character column name. Most of the time while automating many tasks using Excel VBA, it may be required.
Column Name to Column Number is nothing but converting Excel alphabetic character column name to column number. Most of the time while automating many tasks it may be required. Please find the following details about conversion of column name to column number. You can also say column string to column number. Or get alphabetic character to column number using Excel VBA.
We can remove or delete checkbox on the worksheet or userform using ‘Go To Special’ or ‘Select Objects’ or by turning on Design Mode in Developer tab. when we don’t want to place checkbox control on the worksheet or userform we removes from there. Please find the more details and screenshots for clear understanding about remove checkbox control in the following chapter.
We can add CheckBox on Worksheet or UserForm is using VBA with checkbox control and is used to specify or indicate boolean choice. In this section we will see how to add single checkbox or multiple checkboxes on the worksheet or userform using VBA. Please find the more details about add checkbox control using VBA in the following chapter.
Add CheckBox on Worksheet or UserForm is using checkbox control and is used to specify or indicate binary choice. In this section we will see how to add single checkbox or multiple checkboxes on the worksheet or userform. Please find more details about add checkbox control in the following chapter.
Optimize VBA code to run macros faster, simple, easy to understand and efficient way of writing macro. Please find the more details and examples.
Developer Tab in MS Office tools helps programmer to navigate VBA editor, recording macros to automate repetitive tasks and to write VBA programs to develop applications.
Copy worksheet in VBA is used to Copy the worksheet from one location to another location in the same workbook or another new workbook or existing workbook.