简体   繁体   English

如何锁定数据验证单元并在双击时解锁

[英]How to lock Data Validation Cells and unlock on double clicking

I am able to lock all the cells in my sheet and unlock them for editing by double clicking and entering a password. 我可以锁定工作表中的所有单元格,并通过双击并输入密码来解锁它们以进行编辑。

My only problem is the code is not at all working for Data Validation cells. 我唯一的问题是该代码对于Data Validation单元根本不起作用。 Theres no way i can double click and edit data validation values. 我无法双击和编辑数据验证值。

Also is there a way i can have blank cells automatically unprotected and get protected once a value is entered and then double clicking to request password to unlock the cell just like the code im using. 还有一种方法可以让我自动使空白单元格不受保护,并在输入值后得到保护,然后双击以请求密码来解锁单元格,就像我使用的代码一样。

    Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel 
    As Boolean)
    If Target.Value <> "" Then
    changeInput = MsgBox("Do you want to unlock the sheet?", vbYesNo + 
    vbQuestion, "Unlock sheet")
    If changeInput = vbYes Then
        Dim pass As String
        pass = InputBox("Enter Password")
            If pass <> "password" Then
                MsgBox ("Wrong password")
            Else
                ActiveSheet.Unprotect Password:="password"
                Target.Locked = False
            End If
    End If
    End If
    End Sub

    Private Sub Worksheet_Change(ByVal Target As Range)
    Dim cel As Range
    ActiveSheet.Unprotect Password:="password"
    For Each cel In Target
    If cel.Value <> "" Then
        cel.Locked = True
    End If
    Next cel
    ActiveSheet.Protect Password:="password"

    End Sub

The code works on all other cells but i was hoping blank cells would be editable and this code doesnt work on data validation cells. 该代码适用于所有其他单元格,但我希望空白单元格是可编辑的,并且此代码不适用于数据验证单元格。

For the Data-Validation cells try, in your worksheet_change event: 对于data-validation单元,请尝试在worksheet_change事件中:

ActiveSheet.Protect DrawingObjects:=False, Password:="password"

instead of 代替

ActiveSheet.Protect Password:="password"

a way to have blank cells automatically unprotected is to make sure all blank cells are not locked from scratch. 自动保护空白单元格的一种方法是确保所有空白单元格都不会被刮擦锁定。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM