tomsov
New Member
- Joined
- Mar 31, 2017
- Messages
- 24
Hello, I'm trying to clear all sorting fields in a table using VBA Worksheet Activate so that when the user accesses a tab, any filters are removed from the columns and a pre-applied filter to two columns.
The vba applied filters are: 'date order' so that newest date is at the bottom of the list, and if field 1 (column A) contains a '1' then only show data with a '1' in the first column (which to us shows that the job is outstanding).
Here's my code, it does work to a certain extent but if a user sets a filter in column 12 (for example) then this filter remains when I really need it to be cleared. The code does set a '1' field and date order as required.[CustomerTAB] is my table reference name.
Private Sub worksheet_Activate()
ActiveWorkbook.Worksheets("Private Customer").ListObjects("CustomerTAB"). _
Sort.SortFields.Clear
If ActiveSheet.AutoFilterMode Then ActiveSheet.AutoFilter.ShowAllData
ActiveWorkbook.Worksheets("Private Customer").ListObjects("CustomerTAB"). _
Sort.SortFields.Clear
ActiveWorkbook.Worksheets("Private Customer").ListObjects("CustomerTAB"). _
Sort.SortFields.Add2 Key:=Range("CustomerTAB[[#All],[Order Date]]"), _
SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("Private Customer").ListObjects( _
"CustomerTAB").Sort
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
ActiveSheet.ListObjects("CustomerTAB").Range.AutoFilter Field:=1, _
Criteria1:="1"
End Sub
Many thanks
The vba applied filters are: 'date order' so that newest date is at the bottom of the list, and if field 1 (column A) contains a '1' then only show data with a '1' in the first column (which to us shows that the job is outstanding).
Here's my code, it does work to a certain extent but if a user sets a filter in column 12 (for example) then this filter remains when I really need it to be cleared. The code does set a '1' field and date order as required.[CustomerTAB] is my table reference name.
Private Sub worksheet_Activate()
ActiveWorkbook.Worksheets("Private Customer").ListObjects("CustomerTAB"). _
Sort.SortFields.Clear
If ActiveSheet.AutoFilterMode Then ActiveSheet.AutoFilter.ShowAllData
ActiveWorkbook.Worksheets("Private Customer").ListObjects("CustomerTAB"). _
Sort.SortFields.Clear
ActiveWorkbook.Worksheets("Private Customer").ListObjects("CustomerTAB"). _
Sort.SortFields.Add2 Key:=Range("CustomerTAB[[#All],[Order Date]]"), _
SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("Private Customer").ListObjects( _
"CustomerTAB").Sort
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
ActiveSheet.ListObjects("CustomerTAB").Range.AutoFilter Field:=1, _
Criteria1:="1"
End Sub
Many thanks