2018年7月2日月曜日

【マクロ】フィルター状況を調べる

オートフィルターが絞り込まれているか調べる。

Sub Sample7()
    If ActiveSheet.AutoFilterMode Then
        If ActiveSheet.AutoFilter.FilterMode Then
            MsgBox "絞り込まれています"
        Else
            MsgBox "絞り込まれていません"
        End If
    End If
End Sub




Sub Sample8()
    Dim n As Long
    If ActiveSheet.AutoFilterMode Then
        n = ActiveSheet.AutoFilter.Filters.Count
        MsgBox n & "列のフィルタがあります"
    End If
End Sub




上記の方法では、テーブルの場合は機能しない。
そこで
ActiveSheet.AutoFilter.FilterMode を ActiveSheet.ListObjects(1).ShowAutoFilter に変える。 Sub Sample9()
    If ActiveSheet.ListObjects(1).ShowAutoFilter Then
        If ActiveSheet.AutoFilter.FilterMode Then
            MsgBox "絞り込まれています"
        Else
            MsgBox "絞り込まれていません"
        End If
End If
End Sub

これでテーブルでも機能する。

0 件のコメント:

コメントを投稿