Clear Filters code error if nothing is Filtered

gheyman

Well-known Member
Joined
Nov 14, 2005
Messages
2,059
Office Version
  1. 365
Platform
  1. Windows
I have a simple code that clears all the filters/slicers that I tied to a button on the sheet. My problem is that if nothing is filtered and someone clicks the button I get a debug error. Is there a way to not run the code if nothing is filtered or to just not show or have the error popup if nothing is filtered?

Run-time error '1004':
ShowAllData method of worksheet class failed.

Code:
Sub ClearAllFilters()
'GH

    Range("B11").Select
    ActiveSheet.ShowAllData
    
End Sub

Thanks!
 

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.

Alex Blakenburg

MrExcel MVP
Joined
Feb 23, 2021
Messages
3,623
Office Version
  1. 365
Platform
  1. Windows
Replace your ActiveSheet.ShowAllData with this:-

VBA Code:
    If ActiveSheet.FilterMode Then ActiveSheet.ShowAllData

FilterMode will be TRUE if a filter condition has been applied and FALSE otherwise.
 
Solution
Master Excel Bundle

Excel contains over 450 functions, with more added every year. That’s a huge number, so where should you start? Right here with this bundle.

Forum statistics

Threads
1,164,042
Messages
5,835,086
Members
430,340
Latest member
xoxe1337

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