Hiding autofilter arrows not working

2019-09-17 01:27发布

'data is filtered
Sub Filtration(mainsheet As Worksheet, lastrow As Long)

    Application.ScreenUpdating = False

    With mainsheet

        'filters
        .Range("$A$12:$J$" & lastrow).AutoFilter Field:=7, Criteria1:="TRUE" 
        .Range("$A$12:$J$" & lastrow).AutoFilter Field:=10, Criteria1:="TRUE"

        HideAutoFilterDropdowns

    End With

    Application.ScreenUpdating = True

End Sub

Sub HideAutoFilterDropdowns()
    With Range("A12")
        .AutoFilter Field:=1, VisibleDropDown:=False
        .AutoFilter Field:=2, VisibleDropDown:=False
        .AutoFilter Field:=3, VisibleDropDown:=False
        .AutoFilter Field:=4, VisibleDropDown:=False
        .AutoFilter Field:=5, VisibleDropDown:=False
        .AutoFilter Field:=6, VisibleDropDown:=False
        .AutoFilter Field:=7, VisibleDropDown:=False 'problem is here
        .AutoFilter Field:=8, VisibleDropDown:=False
        .AutoFilter Field:=9, VisibleDropDown:=False
        .AutoFilter Field:=10, VisibleDropDown:=False
    End With
End Sub

What happens in the code above is I have a set of data on my sheet and it is filtered by field number 7 and 10 (column G & J). I then want to hide the drop down arrows. It works for 1-6 & 8-10, but when I try to hide column 7 it unfilters the data (counter to what the Filtration function did).

2条回答
甜甜的少女心
2楼-- · 2019-09-17 02:12

You need to leave your Criteria1:= code in the line. Once you remove that Criteria and run HideAutoFilterDropdowns() you are actually telling it to remove the Criteria filter.

With mainsheet

    'filters
    .Range("$A$12:$J$" & lastrow).AutoFilter Field:=7, Criteria1:="TRUE", VisibleDropDown:=False 
    .Range("$A$12:$J$" & lastrow).AutoFilter Field:=10, Criteria1:="TRUE", VisibleDropDown:=False

End With
查看更多
不美不萌又怎样
3楼-- · 2019-09-17 02:14

You can also insert Sub "HideArrows()" found at Hide Arrows in Excel AutoFilter. The original code has been modified to fit your specific needs with comments.

I was having the same issue and wanted to share this solution that was found among numerous opened tabs regarding this topic. Hopefully this will be helpful to others like me who are searching message boards for help and answers.

Sub HideArrows() 

'hides all arrows except column 2
Dim c As Range
Dim i As Integer
i = Cells(1, 1).End(xlToRight).Column
Application.ScreenUpdating = False

For Each c In Range(Cells(1, 1), Cells(1, i))
 'If c.Column <> 2 Then    **** added comment to remove
  c.AutoFilter Field:=c.Column, _
  Visibledropdown:=False
 'End If    **** added comment to remove
Next 

Application.ScreenUpdating = True 
End Sub
查看更多
登录 后发表回答