Quick VBA question: Checking if a filter is applied

reddolls

Board Regular
Joined
Feb 20, 2007
Messages
53
Hi,

I am using

ActiveSheet.ShowAllData

in a piece of VBA and it causes an error if the sheet does not have a filter applied! Is there an if statement I can use to check if the data is filtered?

Thanks - this site is an absolute life saver!

Rich
 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
This should help.
Code:
Sub TurnAutoFilterOn()
'check for filter, turn on if none exists
  If Not ActiveSheet.AutoFilterMode Then
    ActiveSheet.Range("A1").AutoFilter
  End If
End Sub
 
Upvote 0
Hi Rich

You can do this in combination with the AutofilterMode property:

Code:
If Activesheet.AutoFiltermode Then 'autofilter is 'on'
   On Error Resume Next   'turn off error reporting
   Activesheet.ShowAllData
   On Error Goto 0   'turn error reporting back on
End If
 
Upvote 0
the correct property is FilterMode, hence
Code:
If Sheets("archive").FilterMode Then Sheets("archive").ShowAllData
 
Upvote 0
Depends on your definition of "the sheet does not have a filter applied!".

If as the OP appears to want it only if the data is being filtered then you are correct.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,573
Messages
6,120,318
Members
448,956
Latest member
Adamsxl

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