Capture and restore autofilter criteria in Excel 2007 for xlFilterValues operator?

smedz0713

New Member
Joined
Mar 31, 2010
Messages
3
I have also posted this at on old thread - http://www.mrexcel.com/forum/showthread.php?p=2265955

I know this is an old thread but it is the only one I could find that is close to my problem.

Does anyone know if it is possible to discover the criteria used by the user to save and restore the state of an autofilter if Operator:= xlFilterValues?

When I attempt to record a macro to create an autofilter as follows:

start snippet:

Sub Macro2()
'
' Macro2 Macro
'
'
Range("D3").Select
ActiveSheet.ListObjects("Table1").Range.AutoFilter Field:=5, Criteria1:= _
Array("1 - Identify", "2 - Qualified/Contacted", "6 - Close"), Operator:= _
xlFilterValues
ActiveSheet.ListObjects("Table1").Range.AutoFilter Field:=6, Operator:= _
xlFilterValues, Criteria2:=Array(0, "9/30/2010", 1, "4/28/2009", 1, "5/1/2009", 1, _
"10/26/2009", 2, "12/18/2009", 2, "12/31/2009")
End Sub


end snippet:

I can (of course) replay this macro to obtain the same autofilter result as the first time I used the autofilter (to create the recorded macro). However, if I attempt to use the saveautofilter and restoreautofilter functionality as presented by "Peter SSs" in this thread (I have used similar functionality successfully in Excel 2003 for a few years), when I try to access the value of Criteria1 (and Criteria2 if it exists), I receive error 1004 (the wonderfully generic "application-defined or object-defined error").

As expected, attempting to read the values stored in Criteria1 and Criteria2 using the watch window is impossible too. For now, I am checking if the .Operator value = xlFilterValues and, if it is, do nothing.

If anyone has any method to discover or read the filter values referenced via the complex filter that exists if .Operator = xlFilterValues, I would love to hear about it.

Thanks,

Steve
 

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
Hi!
For my purpose, to unhide the rows that the Autofilter made, do the stuff I want and then set the Autofilter on with it's specific filterings set again.
This works in Office 2010 with multiple selected filters!

I slightly changed the code above
Sub ReDoAutoFilter()
Dim w As Worksheet
Dim filterArray()
Dim currentFiltRange As String
Dim col As Integer

Set w = ActiveSheet


' Capture AutoFilter settings
With w.AutoFilter
currentFiltRange = .Range.Address
With .Filters
ReDim filterArray(1 To .Count)
For f = 1 To .Count
With .Item(f)
If .On Then
filterArray(f) = Array() ' this to type caste to an array type!
filterArray(f) = .Criteria1
End If
End With
Next f
End With
End With

'Remove AutoFilter
w.AutoFilterMode = False

' Your code here

' Restore Filter settings
For col = 1 To UBound(filterArray(), 1)
If Not IsEmpty(filterArray(col)) Then
w.Range(currentFiltRange).AutoFilter Field:=col, Criteria1:=filterArray(col)
End If
Next col
End Sub



Br Stefan
 
Upvote 0
Thanks Stefan,

I tried your code on Excel 2007 and it only retained the first element from filterArray(col) in Criteria1 for any columns that have filter "Operator" = xlFilterValues; however, your code gave me a an idea and I have found that the following code (to simulate polymorphism) works for complex filters in Excel 2007 where AutoFilter is turned on:

Two class modules are required
myFilterDetailClass
myFilterClass

CLASS: myFilterDetailClass

Dim Criteria1() As Variant
Public Criteria2 As Variant
Public Count As Integer
Public IsOn As Boolean
Public Operator As XlAutoFilterOperator

Public Sub SetCriteria1(argList As Variant)
If IsArray(argList) Then
Criteria1 = argList
Else
ReDim Criteria1(0)
Criteria1(0) = argList
End If
End Sub

Public Function GetCriteria1() As Variant
GetCriteria1 = Criteria1
End Function

CLASS: myFilterClass
Public myFilter As New myFilterDetailClass
Public myIndex As Integer

CODE SNIPPET:

Sub ReDoAutoFilterAsCollection()
'
Dim w As Worksheet
Dim filterCollection As New Collection
Dim myFilterInst As myFilterClass
Dim currentFiltRange As String
Dim col As Integer, f As Integer
Set w = ActiveSheet
' Capture AutoFilter settings
With w.AutoFilter
currentFiltRange = .Range.Address
With .Filters
For f = 1 To .Count
With .Item(f)
If .On Then
Set myFilterInst = New myFilterClass
myFilterInst.myIndex = f
myFilterInst.myFilter.Count = .Count
If .Operator = xlFilterValues Then
myFilterInst.myFilter.SetCriteria1 (.Criteria1)
Else
myFilterInst.myFilter.SetCriteria1 (.Criteria1)
myFilterInst.myFilter.Criteria2 = .Criteria2
End If
myFilterInst.myFilter.IsOn = .On
myFilterInst.myFilter.Operator = .Operator

filterCollection.Add Item:=myFilterInst, Key:=CStr(f)

End If
End With
Next f
End With
End With
'Remove AutoFilter
w.AutoFilterMode = False
w.AutoFilter.ShowAllData
' Your code here
' Restore Filter settings
For Each myFilterInst In filterCollection
If myFilterInst.myFilter.Operator = xlFilterValues Then
w.Range(currentFiltRange).AutoFilter Field:=myFilterInst.myIndex, Criteria1:=myFilterInst.myFilter.GetCriteria1(), Operator:=myFilterInst.myFilter.Operator
Else
w.Range(currentFiltRange).AutoFilter Field:=myFilterInst.myIndex, Criteria1:=myFilterInst.myFilter.GetCriteria1(), Criteria2:=myFilterInst.myFilter.Criteria2, Operator:=myFilterInst.myFilter.Operator
End If
Next myFilterInst
' Cleanup here
Do While filterCollection.Count > 0
filterCollection.Remove (1)
Loop
Set myFilterInst = Nothing
Set filterCollection = Nothing
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,136
Messages
6,123,247
Members
449,093
Latest member
Vincent Khandagale

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top