Clear Filters code error if nothing is Filtered

gheyman

Well-known Member
Joined
Nov 14, 2005
Messages
2,332
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

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
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.
 
Upvote 0
Solution

Forum statistics

Threads
1,213,506
Messages
6,114,024
Members
448,543
Latest member
MartinLarkin

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