schielrn
Well-known Member
- Joined
- Apr 4, 2007
- Messages
- 6,941
This may or may not be the best way to handle this, but it surely was nice for Excel allowing us to select multiple criteria in an autofilter. But what would have been great would to have it filter on the opposite very simply by pushing a button or something.
Say you have a column like:
North
North
South
East
South
West
West
North
You then filter on north and south. It would be nice to push a button and filter on east and west and not north and south for a lot of things I work on. I know I could add a column that separated the 2, but that is not my idea.
So I have came up with this code that works only with the active column because I do not know how to determine which filter columns have criteria on them. It also will not work if other columns are filtered. It only works with one filtered column with the way I am building the array based on hidden rows.
I was trying to expand on this code here that worked with pre-2007 Excel:
http://www.j-walk.com/SS/excel/usertips/tip044.htm
But I have been struggling getting any results with that. It would be nice to be able to capture the array of variables that autofilter is using for the criteria, such as this is just recording a basic macro:
I would love to be able to just capture criteria1 an dmaybe do something like "<>" or taking those out of my full array.
Basically it is looking at the column of the activecell to know what to apply the macro to. Has anyone tried working with this or any criticism or better approaches would be appreciated.
Thanks,
Rob
Say you have a column like:
North
North
South
East
South
West
West
North
You then filter on north and south. It would be nice to push a button and filter on east and west and not north and south for a lot of things I work on. I know I could add a column that separated the 2, but that is not my idea.
So I have came up with this code that works only with the active column because I do not know how to determine which filter columns have criteria on them. It also will not work if other columns are filtered. It only works with one filtered column with the way I am building the array based on hidden rows.
I was trying to expand on this code here that worked with pre-2007 Excel:
http://www.j-walk.com/SS/excel/usertips/tip044.htm
But I have been struggling getting any results with that. It would be nice to be able to capture the array of variables that autofilter is using for the criteria, such as this is just recording a basic macro:
Code:
ActiveSheet.Range("$A$1:$E$14").AutoFilter Field:=1, Criteria1:=Array("df", _
"dsfg", "gds", "gsd", "sfd"), Operator:=xlFilterValues
Code:
Sub OppositeFilter()
Dim arr() As String
For Each c In ActiveSheet.AutoFilter.Range.Resize(, 1).Offset(, ActiveCell.Column - 1)
On Error Resume Next
If c.EntireRow.Hidden Then
If Not WorksheetFunction.Match(c.Value, arr(), 0) > 0 Then
ReDim Preserve arr(counter)
arr(counter) = c.Value
counter = counter + 1
End If
End If
Next c
If counter = 0 Then Exit Sub
ActiveSheet.AutoFilter.Range.AutoFilter Field:=ActiveCell.Column, Criteria1:=arr(), Operator:=xlFilterValues
End Sub
Thanks,
Rob