Toggle Auto Filter On and Off with specific parameters

fishandtril

New Member
Joined
Nov 15, 2022
Messages
13
Office Version
  1. 365
  2. 2021
Platform
  1. Windows
I am using the below code to toggle on and off filter showing current month.
The code works with the exception it leaves the sheet without filters. I would like thist to leave AutoFilterMode=True once run.

VBA Code:
Sub Toggle_Current_Month()
'
' Macro to filter current month basis BOL Date
'
   With ActiveSheet
        If .AutoFilterMode Then
            .AutoFilterMode = False
        Else
            ActiveSheet.Range("$A$96:$CM$1500").AutoFilter Field:=16, Criteria1:=xlFilterThisMonth, Operator:=xlFilterDynamic
        End If
    End With
End Sub
 
Last edited by a moderator:

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
Welcome to the MrExcel board!

When posting vba code in the forum, please use the available code tags. It makes your code much easier to read/debug. My signature block below has more details. I have added the tags for you this time. 😊

See if this does what you want.

VBA Code:
Sub Toggle_Current_Month_v2()
   With ActiveSheet
        If .FilterMode Then
            .ShowAllData
        Else
            .Range("$A$96:$CM$1500").AutoFilter Field:=16, Criteria1:=xlFilterThisMonth, Operator:=xlFilterDynamic
        End If
    End With
End Sub
 
Upvote 0
Welcome to the MrExcel board!

When posting vba code in the forum, please use the available code tags. It makes your code much easier to read/debug. My signature block below has more details. I have added the tags for you this time. 😊

See if this does what you want.

VBA Code:
Sub Toggle_Current_Month_v2()
   With ActiveSheet
        If .FilterMode Then
            .ShowAllData
        Else
            .Range("$A$96:$CM$1500").AutoFilter Field:=16, Criteria1:=xlFilterThisMonth, Operator:=xlFilterDynamic
        End If
    End With
End Sub
Thanks for the Help. Works Perfectly :)
 
Upvote 0
You're welcome. Thanks for the follow-up. :)
 
Upvote 0

Forum statistics

Threads
1,215,507
Messages
6,125,207
Members
449,214
Latest member
mr_ordinaryboy

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top