add an extra column to auto filter

smiler44

Registered User.
Local time
Today, 00:27
Joined
Jul 15, 2008
Messages
690
I have received a spread sheet with an auto filter already attached and filtered. I now want to add another column to the auto filter but do not want to remove the existing auto filter, as I don't know what the filter criteria is.

I have some code that works out the existing auto filter, removes it and reapplies it but only reapplies if to the same settings as it was when it removed it so wont filter my extra column.

I thought this would do it be when the original filter is reapplied there is only the header row visible

Code:
 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, 1 To 3)
            For f = 1 To .Count
                With .Item(f)
                    If .On Then
                        filterArray(f, 1) = .Criteria1
                        If .Operator Then
                            filterArray(f, 2) = .Operator
                            filterArray(f, 3) = .Criteria2 'simply delete this line to make it work in Excel 2010
                        End If
                    End If
                End With
            Next f
        End With
    End With
     'Remove AutoFilter
    w.AutoFilterMode = False
     ' Your code here
    'MsgBox ("Do what ever you need here")
    Columns("D:D").Select
    Selection.AutoFilter
     ' Restore Filter settings
    For col = 1 To UBound(filterArray(), 1)
        If Not IsEmpty(filterArray(col, 1)) Then
            If filterArray(col, 2) Then
                w.Range(currentFiltRange).AutoFilter field:=col, _
                Criteria1:=filterArray(col, 1), _
                Operator:=filterArray(col, 2), _
                Criteria2:=filterArray(col, 3)
            Else
                w.Range(currentFiltRange).AutoFilter field:=col, _
                Criteria1:=filterArray(col, 1)
            End If
        End If
    Next col
End Sub


why do I put myself through this pain?

smiler44
 

Users who are viewing this thread

Back
Top Bottom