VBA unexpected error

Mr2017

Well-known Member
Joined
Nov 28, 2016
Messages
644
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
Hi

I've got some code which clears a filter if something has been filtered in the sheet.

However, it shows an error if the filter has NOT been applied and all data is already showing!

Does anyone know why this may be?

VBA Code:
Sub ClearData



Sheet12.Activate
If ActiveSheet.AutoFilterMode Then ActiveSheet.ShowAllData
Range("d7:d10000").Select
Selection.ClearContents


End Sub

The error I get is "ShowAllData method of Worksheet class failed"
 

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.
AutoFilterMode just tells you if there is an autofilter available on the sheet, not whether it's actually filtering anything. You can use:

Code:
If ActiveSheet.AutoFilterMode Then If Activesheet.Filtermode then ActiveSheet.ShowAllData

or use the On Error Resume Next approach.
 
Upvote 0
Great!!!

Thank you all for your suggestions!

I have used them to fix the problem!

Thanks again!!
 
Upvote 0

Forum statistics

Threads
1,214,790
Messages
6,121,607
Members
449,037
Latest member
Arbind kumar

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