我有以下代码来保护一张纸。排序工作正常,但过滤不会,即使AllowFiltering为True。有什么建议阻止过滤工作吗?
我尝试将Userinterfaceonly设置为true和false,但两者都没有任何区别。同样,EnableSelection = xlUnlockedCells没有帮助。
Sub LockCols()
Dim Sh As Worksheet
Application.EnableEvents = False
Application.ScreenUpdating = False
For Each Sh In ThisWorkbook.Worksheets
'2 other If/End If statements protecting other sheets
If Sh.Name = "Combined" Then
Sh.Activate
ActiveSheet.Unprotect Password:="ABC123"
ActiveSheet.Cells.Locked = False 'True prevents sorting and filtering
ActiveSheet.Protect Password:="ABC123", DrawingObjects:=True, Contents:=True, Scenarios:=True, _
Userinterfaceonly:=True, _
AllowFiltering:=True, _
AllowSorting:=True, _
AllowInsertingColumns:=False, _
AllowDeletingColumns:=False, _
AllowInsertingRows:=False, _
AllowDeletingRows:=False, _
AllowFormattingCells:=False, _
AllowFormattingColumns:=True, _
AllowFormattingRows:=True, _
AllowInsertingHyperlinks:=False
End If
Next
Application.EnableEvents = True
Application.ScreenUpdating = True
End Sub
答案 0 :(得分:0)
你可以尝试像这一样小的东西:
Sub TestMe()
ActiveSheet.Protect Password:="ABC123", AllowFiltering:=True
End Sub
看看它是否有效。如果确实如此,我猜可能在循环中ActiveSheet
存在一些问题。
答案 1 :(得分:0)
不确定是否对任何人有帮助,但是如果您进入documentation,AllowFiltering会以这种方式工作,则必须先设置自动过滤器,然后再保护工作表。