我有以下代码,它删除过滤器,然后从选定的单元格重新应用它们:
Range("A10:AM10").Select
Selection.AutoFilter
Selection.AutoFilter
我遇到的问题是,当工作表受到保护并且我尝试运行代码时出错,那么有没有办法允许在 protected 工作表上打开和关闭过滤器?
最佳答案
你好吗?
这取决于保护的设置。当您保护工作表时,有一个选项允许过滤(自动过滤)。
如果您是保护工作表的人,您可以使用任何工作表的 保护 方法的参数来设置保护选项。以下是保护方法的参数:
sheetName.Protect(Password, DrawingObjects, Contents, Scenarios, UserInterfaceOnly, AllowFormattingCells, AllowFormattingColumns, AllowFormattingRows, AllowInsertingColumns, AllowInsertingRows, AllowInsertingHyperlinks, AllowDeletingColumns, AllowDeletingRows, AllowSorting, AllowFiltering, AllowUsingPivotTables)
如果您希望能够仅通过代码实现过滤器,则可以将 UserInterfaceOnly 参数设置为 TRUE。保护将仅阻止用户更改。代码更改将完全被允许。
sheetName.Protect UserInterfaceOnly:=True
或者,如果您想允许用户从界面过滤工作表的内容,只需将 AllowFiltering 参数设置为 TRUE 以及 UserInterfaceOnly 参数。
sheetName.Protect UserInterfaceOnly:=True, AllowFiltering:=True
您的代码应该使用这两个选项。
希望能帮助到你!
关于VBA:如何允许在 protected 工作表上打开和关闭过滤器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35035776/