简体   繁体   English

输入值后锁定单元格,可以使用正确的密码(VBA)解锁

[英]Lock cell after inputting value, possibility to unlock with correct password (VBA)

We have an excel sheet which serves to input data. 我们有一个用于输入数据的Excel工作表。 To protect the data, it must be locked after a value is entered. 为了保护数据,必须在输入值后将其锁定。 After mistakes or for corrections later on, it must be possible to change the value after entering a password. 在输入错误或以后进行更正后,必须可以在输入密码后更改该值。

I've been working with a protected sheet, and everytime I try to edit a cell's value, I get a message telling me to unprotect the sheet. 我一直在使用受保护的工作表,每次尝试编辑单元格的值时,都会收到一条消息,告诉我取消保护工作表。 As I want the password things to be taken care of in VBA, I wanted an event to be triggered. 因为我希望在VBA中处理密码问题,所以我希望触发一个事件。 The problem is, the only event possible is the worksheet_change event. 问题是,唯一可能的事件是worksheet_change事件。 Unfortunately, the event is never triggered as I get the message saying to unprotect the sheet in order to change the value. 不幸的是,该事件从未触发,因为我收到一条消息,要求取消保护工作表以便更改值。

I've come up with a temporary solution, but is not 100% user friendly: 我已经提出了一个临时解决方案,但并非100%用户友好:

Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
    If Target.Value <> "" Then
    Dim pass As String
        pass = InputBox("Enter Password")
        If pass <> "password" Then
            MsgBox ("Wrong password")
        Else
            Sheets("Blad1").Unprotect password:="password"
            Target.Locked = False
            Target.Value = InputBox("Enter your input")
            Target.Locked = True
            Sheets("Blad1").Protect password:="password"
        End If
    End If
End Sub

I catch the double click event here, which is a before event and is better suited for my needs. 我在这里捕获了双击事件,这是一个之前的事件,更适合我的需求。 This changes the value like it should, but you have to enter the value through an inputbox, which limits to cell linking functionality. 这会像应有的那样更改值,但是您必须通过输入框输入值,这限制了单元链接功能。 It also keeps on generating the message "unprotect the sheet in order to make edits...". 它还继续生成消息“取消保护工作表以便进行编辑...”。

  • Is there another approach/solution for my original problem? 我最初的问题是否还有其他方法/解决方案?
  • Is there an event better suited for my needs (something like before_change)? 是否有更适合我需要的活动(例如before_change之类的活动)?
  • If above questions cannot be answered, is it possible to get rid of the message to unprotect the sheet? 如果以上问题无法回答,是否有可能摆脱保护纸的信息?

See answer for detailed solution. 请参阅答案以获取详细解决方案。

Eventually, I solved it by adding the Worksheet_BeforeDoubleClick event. 最终,我通过添加Worksheet_BeforeDoubleClick事件解决了它。 When they double click on a non empty cell, they are prompted if they want to unlock the sheet. 当他们双击一个非空单元格时,将提示他们是否要解锁工作表。 If they click yes, they have to enter the password and then have access to the cells input. 如果单击“是”,则必须输入密码,然后才能访问单元格输入。 To set this up, you must block all cells that may not be changed without password (cells with formula's, headers,...) and leave empty cells unblocked. 要进行此设置,您必须阻止所有没有密码就无法更改的单元格(具有公式,标题等的单元格),并使空白单元格保持畅通。 Afterwards, protect the sheet with the password. 然后,用密码保护纸张。 Don't forget to put a password on the VBA, or a user who knows how to access it may see the password in the code. 不要忘记在VBA上输入密码,否则知道如何访问该密码的用户可能会在代码中看到该密码。

Here's the code I used in my final version: 这是我在最终版本中使用的代码:

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

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

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