I am trying create a button that would remove filters applied to one or more columns in a single click. But as I have a protected sheets I need to unprotect and protect sheets in the code for this work This method worked but it took about 2 sec as I have 4 protected sheets. Then I got the idea to use an If condition.
Sub Resetauto()
On Error GoTo Handler
Application.DisplayAlerts = False
Application.ScreenUpdating = False
If ActiveSheet.Name = "Sheet3" Then
Sheets("Sheet3").Unprotect Password:="pass"
ActiveWorkbook.ActiveSheet.ListObjects(1).Sort.SortFields _
.Clear
ActiveSheet.ShowAllData
Sheets("Sheet3").Protect _
Password:="pass", _
UserInterfaceOnly:=True, _
AllowFiltering:=True, _
AllowSorting:=True, _
AllowUsingPivotTables:=True
ElseIf ActiveSheet.Name = "Sheet1" Then
Sheets("Sheet1").Unprotect Password:="pass"
ActiveWorkbook.ActiveSheet.ListObjects(1).Sort.SortFields _
.Clear
ActiveSheet.ShowAllData
Sheets("Sheet1").Protect _
Password:="pass", _
UserInterfaceOnly:=True, _
AllowFiltering:=True, _
AllowSorting:=True, _
AllowUsingPivotTables:=True
ElseIf ActiveSheet.Name = "Sheet4" Then
Sheets("Sheet4").Unprotect Password:="pass"
ActiveWorkbook.ActiveSheet.ListObjects(1).Sort.SortFields _
.Clear
ActiveSheet.ShowAllData
Sheets("Sheet4").Protect _
Password:="Pass", _
UserInterfaceOnly:=True, _
AllowFiltering:=True, _
AllowSorting:=True, _
AllowUsingPivotTables:=True
Else
Sheets("Sheet5").Unprotect Password:="pass"
ActiveWorkbook.ActiveSheet.ListObjects(1).Sort.SortFields _
.Clear
ActiveSheet.ShowAllData
Sheets("Sheet5").Protect _
Password:="pass", _
UserInterfaceOnly:=True, _
AllowFiltering:=True, _
AllowSorting:=True, _
AllowUsingPivotTables:=True
End If
Application.DisplayAlerts = True
Application.ScreenUpdating = True
Exit Sub
Handler:
Sheets("Sheet1").Protect _
Password:="pass", _
UserInterfaceOnly:=True, _
AllowFiltering:=True, _
AllowSorting:=True, _
AllowUsingPivotTables:=True
Sheets("Sheet2").Protect _
Password:="pass", _
UserInterfaceOnly:=True, _
AllowFiltering:=True, _
AllowSorting:=True, _
AllowUsingPivotTables:=True
Sheets("Sheet3").Protect _
Password:="pass", _
UserInterfaceOnly:=True, _
AllowFiltering:=True, _
AllowSorting:=True, _
AllowUsingPivotTables:=True
Sheets("Sheet5").Protect _
Password:="pass", _
UserInterfaceOnly:=True, _
AllowFiltering:=True, _
AllowSorting:=True, _
AllowUsingPivotTables:=True
Exit Sub
End Sub
This code doesn't do anything, My cursor rotates for 2 sec..It doesn't remove the filter
Is there something wrong in my code??
Assuming no errors being ignored by the OnError handler, my suggestion would be to replace ElseIf with separate If statements. I find those to be a trap.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.