clear Autofilter if on

howard

Well-known Member
Joined
Jun 26, 2006
Messages
6,566
Office Version
  1. 2021
Platform
  1. Windows
I have data on sheet2 and have written code to clear filter if on, but the code does not do this

It would be appreciated if some would test my code and amend

Code:
Sub Clear_Data()
With Sheets(2)
Dim LR As Long
LR = .Cells(.Rows.Count, "A").End(xlUp).Row

Sheets(2).Select

If ActiveSheet.FilterMode Then
Cells.autofilter
End If

   
.Range("A2:AZ" & LR).ClearContents
End With

   
End Sub
 

Excel Facts

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.
If you aren't dealing with Tables, you can just use:

Code:
On error resume next
Sheets(2).Showalldata
On error goto 0
 
Upvote 0
Thanks for the help Rory


I have just picked up that some of the data is not being cleared from row 10678.


Please check my code & advise
 
Upvote 0
Your code assumes that you can use column A to determine the last row of data. It sounds like that is not the case, so you could use this instead:

Code:
LR = .Usedrange.Row + .Usedrange.Rows.Count - 1
 
Upvote 0
Thanks Rory. Something new I have learnt
 
Upvote 0

Forum statistics

Threads
1,215,363
Messages
6,124,505
Members
449,166
Latest member
hokjock

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