Excel VBA - 过滤用密码锁定的数据

Excel VBA - Filter data that is locked with password

几天来我一直被以下问题困扰:

我想在特定行中使用密码锁定数据,然后仍然能够对该数据使用过滤器。

enter image description here

这是我到目前为止得到的,但由于某种原因它不起作用。

欢迎任何意见!!!

If logbook.Cells(row_index, 1) <> "" Then
    ActiveSheet.Unprotect password:="mypassword"
    logbook.Cells(row_index, 11).Value = "YES"
    logbook.Cells(row_index, 1).EntireRow.Locked = True
    ActiveSheet.Protect password:="mypassword", AllowFiltering:=True         
Else
    Unload Me
End If

不确定我是否完全理解你的问题,但这有效:

Sub Test()

    'worksheet is protected
    'read only operations

    Sheets("tbl").Select
    ActiveSheet.Unprotect Password:="mypassword"

    'changes to worksheet

    Sheets("Tabelle1").Select
    ActiveSheet.Protect Password:="mypassword"

End Sub

如果您需要通过VBA更改数据,则无需取消保护。

只需使用 UserInterfaceOnly:=True 并使用 AllowFiltering:=True,您仍然可以过滤数据。像这样:

ActiveSheet.Protect Password:="mypassword", AllowFiltering:=True, UserInterfaceOnly:=True