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

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.

Datsmart

Well-known Member
Joined
Jun 19, 2003
Messages
7,985
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
 

Richard Schollar

MrExcel MVP
Joined
Apr 19, 2005
Messages
23,707
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
 

clemoPCL

New Member
Joined
Jan 6, 2017
Messages
1
the correct property is FilterMode, hence
Code:
If Sheets("archive").FilterMode Then Sheets("archive").ShowAllData
 

MARK858

MrExcel MVP
Joined
Nov 12, 2010
Messages
14,684
Office Version
  1. 365
Platform
  1. Windows
  2. Mobile
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:

Forum statistics

Threads
1,181,649
Messages
5,931,214
Members
436,784
Latest member
amuljono

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
Top