Macro to check if column is filtered in table and apply filter if not

kmach60

New Member
Joined
Jan 11, 2020
Messages
4
Office Version
  1. 365
Platform
  1. Windows
Hi everyone,

I'm trying to make a macro that checks column "Sun" on my table "Table1" to see if there is a filtered applied, if there is not I want to apply this filter. I'm also trying to make the macro dynamic by searching column headers to find the right column since my table changes.

This is what I have so far but was hoping someone could lead me in the right direction.

VBA Code:
    Dim lo As ListObject
    Dim iCol As Long
    
    Set lo = Sheet1.ListObjects(1)
    iCol = lo.ListColumns("Sun").Index
    lo.Range.AutoFilter Field:=iCol, Criteria1:="<>"

All this macro does so far is apply the filter, how do I add in the checking to see if the filter is applied part? I've tried to search but I've only found answers that don't apply to tables.

Thank you in advance!
 

Excel Facts

When they said...
When they said you are going to "Excel at life", they meant you "will be doing Excel your whole life".
Sorry everyone, I actually need the macro to toggle the filter on and off, not just apply filter if not applied. Any help would be appreciated!
 
Upvote 0
VBA Code:
Sheet1.AutoFilterMode = True
VBA Code:
Sheet1.AutoFilterMode = False
 
Upvote 0
How about
VBA Code:
Sub kmach()
   Dim lo As ListObject
   Dim iCol As Long
   
   Set lo = Sheet1.ListObjects(1)
   iCol = lo.ListColumns("Sun").Index
   If lo.AutoFilter.FilterMode Then
     lo.AutoFilter.ShowAllData
   Else
      lo.Range.AutoFilter Field:=iCol, Criteria1:="<>"
   End If
End Sub
 
Upvote 0
Sorry everyone, I actually need the macro to toggle the filter on and off, not just apply filter if not applied. Any help would be appreciated!
VBA Code:
Sub ToggleFilter() ' Uncomment one of these and correct as necessary
  'Sheet1.AutoFilterMode = Not Sheet1.AutoFilterMode  'Either by object name reference
  'Sheets("SheetName").AutoFilterMode = Not Sheets("SheetName").AutoFilterMode  'Or by Sheet name reference
End Sub
 
Upvote 0
Read this code, I hope can help you:

This code works like this ==> Check if FIlter on specific Column (indicator) is ON, then removes it, else applies it
1615732943793.png
.

1615732943793.png
 
Upvote 0

Forum statistics

Threads
1,214,959
Messages
6,122,476
Members
449,087
Latest member
RExcelSearch

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