2015年3月30日 星期一

Excel VBA Add Table AutoFilter cancel AutoFilter sorting

This Excel tutorial explains how to use Excel VBA to add Table AutoFilter, cancel Table AutoFilter, sorting Table


Excel VBA Add Table AutoFilter or Cancel AutoFilter


Using VBA to add AutoFilter or cancel Auto Filter is very simple if you know the code, but you should note that Excel does not treat Worksheet AutoFilter the same way as Table AutoFilter.


This article only talks about Table AutoFilter.


For Worksheet AutoFilter, click here.


Excel VBA Add Table AutoFilter


You can only have one Auto Filter in one Worksheet. However if you have Table, you can have one Worksheet Auto Filter, plus one Filter for each Table. AutoFilter in Table is an Object for each Table.


Table is a ListObjects, you can access the AutoFilter of Table by specifying Table name ListObjects(“Table_name”) or through ListObjects Array. If you have two Table in the worksheet, the first added Table is ListObjects(1) and the other is ListObjects(2).


Add Table AutoFilter


The below code add AutoFilter for ListObjects(1). If AutoFilter is already present, nothing will happen.


Dim LstObj As ListObject
Set LstObj = ActiveSheet.ListObjects(1)
LstObj.ShowAutoFilter = True

Cancel Table AutoFilter


The below code cancel AutoFilter for ListObjects(1). If AutoFilter is not present, nothing will happen.


Dim LstObj As ListObject
Set LstObj = ActiveSheet.ListObjects(1)
LstObj.ShowAutoFilter = False

Unhide filtered data


The below code unhide AutoFilter for ListObjects(1).


Dim LstObj As ListObject
Set LstObj = ActiveSheet.ListObjects(1)
If LstObj.AutoFilter.FilterMode Then
LstObj.AutoFilter.ShowAllData
End If

Apply criteria to Table AutoFilter


The syntax is exactly the same as Worksheet AutoFilter because both of them use AutoFilter Method of Range. the only difference is that you have to assess the Range Object of ListObject first.


The below example shows how to assess Range of LstObj in order to use AutoFilter Method.


LstObj.Range.AutoFilter Field:=1, Criteria1:=”A”, Operator:=xlOr, Criteria2:=”D”


Read the syntax of AutoFilter Method before you read on.


























NameRequiredlData TypeDescription
FieldOptionalVariantThe integer offset of the field on which you want to base the filter (from the left of the list; the leftmost field is field one).
Criteria1OptionalVariantThe criteria (a string; for example, “101”). Use “=” to find blank fields, or use “<>” to find nonblank fields. If this argument is omitted, the criteria is All. If Operator is xlTop10Items, Criteria1 specifies the number of items (for example, “10”).
OperatorOptionalXlAutoFilterOperator One of the constants of XlAutoFilterOperator specifying the type of filter.




































NameValueDescription
xlAnd1Logical AND of Criteria1 and Criteria2.
xlBottom10Items4Lowest-valued items displayed (number of items specified in Criteria1).
xlBottom10Percent6Lowest-valued items displayed (percentage specified in Criteria1).
xlFilterCellColor8Color of the cell
xlFilterDynamic11Dynamic filter
xlFilterFontColor9Color of the font
xlFilterIcon10Filter icon
xlFilterValues7Filter values
xlOr2Logical OR of Criteria1 or Criteria2.
xlTop10Items3Highest-valued items displayed (number of items specified in Criteria1).
xlTop10Percent5Highest-valued items displayed (percentage specified in Criteria1).
Criteria2OptionalVariantThe second criteria (a string). Used with Criteria1 and Operator to construct compound criteria.
VisibleDropDownOptionalVariantTrue to display the AutoFilter drop-down arrow for the filtered field. False to hide the AutoFilter drop-down arrow for the filtered field. True by default.

The below examples summarize all the basics you need to know about AutoFilter Criteria.
















ExampleExplanation
LstObj.Range.AutoFilter Field:=1, Criteria1:=”b”Filter text criteria
LstObj.Range.AutoFilter Field:=1, Criteria1:=”b*”Filter text criteria with Wildcard
LstObj.Range.AutoFilter Field:=2, Criteria1:=”>10″Filter number criteria
LstObj.Range.AutoFilter Field:=1, Criteria1:=”b”, _

Operator:=xlOr, Criteria2:=”c”
Filter two text criteria using OR, which means the result shows b and c. Note that you can only have 2 criteria at most
LstObj.Range.AutoFilter Field:=1, Criteria1:= Array(“a”, “b”, “c”), Operator:=xlFilterValuesFilter two text criteria using Array
LstObj.Range.AutoFilter Field:=1, Criteria1:=”b”
LstObj.Range.AutoFilter Field:=2, Criteria1:=”>10″
Filter two different Fields

Apply Table AutoFilter sorting


Before applying sorting, you should clear previous sorting first. To clear sorting in AutoFilter, use Sort.SortFields.Clear Method. Note that clear sorting does not reverse the data to the original order.


To tell AutoFilter how you want to sort the data, use Sort.SortFields.Add Method. (no data is sorted at this point)


























NameRequired/OptionalData TypeDescription
KeyRequiredRangeSpecifies a key value for the sort.
SortOnOptionalVariantThe field to sort on.
OrderOptionalVariantSpecifies the sort order.
CustomOrderOptionalVariantSpecifies if a custom sort order should be used.
DataOptionOptionalVariantSpecifies the data option.

Finally, use Sort.Apply Method  to apply on the sort states.


The below example sorts data in Ascending Order in column A.


Dim LstObj As ListObject
Set LstObj = ActiveSheet.ListObjects(1)
LstObj.Sort.SortFields.Clear
LstObj.Sort.SortFields.Add Key:=Range("A:A"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
LstObj.Sort.Apply

Reset Table Filter


The below code unhide all hidden data and cancel sorting.


Dim LstObj As ListObject
Set LstObj = ActiveSheet.ListObjects(1)
LstObj.ShowAutoFilter = False
LstObj.ShowAutoFilter = True
LstObj.Sort.SortFields.Clear

 



Excel VBA Add Table AutoFilter cancel AutoFilter sorting

沒有留言:

張貼留言