Hiding autofilter arrows not working

我们两清 提交于 2019-12-12 04:25:08

问题


'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).


回答1:


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



回答2:


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


来源:https://stackoverflow.com/questions/27801531/hiding-autofilter-arrows-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!