clear Autofilter if on

howard

Well-known Member
Joined
Jun 26, 2006
Messages
6,603
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

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.
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

Forum statistics

Threads
1,224,802
Messages
6,181,054
Members
453,014
Latest member
Chris258

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