VBA代码用于保护工作表但允许筛选。

huangapple go评论149阅读模式
英文:

VBA code to protect worksheet but allow filtering

问题

以下是代码的中文翻译部分:

Private Sub Workbook_Open()
   MsgBox "已更新文件至 3 月 23 日"
    With Sheets("3 月 23 日")
        .Protect AllowFiltering:=True
        .Protect Password:="密码", UserInterfaceOnly:=True
        .EnableOutlining = True
    End With
End Sub
英文:

I have a worksheet where i want to limit changes made by users but also allow columns to be grouped so i used the following code. I then realised i can't filter on the protected sheet so amended the code but it's still not allowing to filter and not sure where i have gone wrong as new to vba

    Private Sub Workbook_Open()
   MsgBox "File updated for March 23"
    With Sheets("March 23")
        .Protect AllowFiltering:=True
        .Protect Password:="Secret", UserInterfaceOnly:=True
        .EnableOutlining = True
    End With
    End Sub

答案1

得分: 1

您可以通过设置 AllowFiltering:=True 来允许用户筛选数据,但是在保护工作表之前,您需要激活筛选(显示筛选按钮),因为此命令在受保护的工作表上不可用:

Private Sub Workbook_Open()
    With Sheets("March 23")
        If Not .AutoFilterMode Then .AutoFilter
        .Protect AllowFiltering:=True, _
                 Password:="Secret", _
                 UserInterfaceOnly:=True
        .EnableOutlining = True
    End With
End Sub
英文:

You can allow the users to filter the data by setting AllowFiltering:=True, however, you will need to activate the filter (show the filter buttons) before the sheet is protected as this command is not available on a protected sheet:

Private Sub Workbook_Open()
    With Sheets("March 23")
        If Not .AutoFilterMode then .AutoFilter
        .Protect AllowFiltering:=True, _
                 Password:="Secret", _
                 UserInterfaceOnly:=True
        .EnableOutlining = True
     End With
 End Sub

huangapple
  • 本文由 发表于 2023年3月8日 17:28:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/75671323.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定