个人工作表密码

我在Excel中使用了“保护工作表”选项,另外,我想要求用户在单击每个表格时input密码。 每张纸上都有一个特定的密码。 如果您不知道密码,我不希望用户能够修改表单中的任何内容。 他们可以打印,但不能修改任何东西。 我有一个工作表的密码,但我使用的代码不工作…也许这是太基本…有人可以请协助?

Private Sub Workbook_SheetActivate(ByVal Sh As Object) Dim Msg As String Dim UserEntry As Variant Msg = InputBox("What is the password?") Do Sheet1.Activate UserEntry = InputBox(Msg) If UserEntry = "test" Then Exit Sub If UserEntry = False Then Msg = "Invalid Password!" Msg = Msg & vbNewLine Msg = Msg & "What is the password?" Loop Sheet1.Activate = UserEntry End Sub 

虽然它可能取决于您的Excel版本(我在2007年testing过),只需右键单击该选项卡并select“ 保护表 ”。 您可以分别设置每张纸的密码。

用户只需转到function区的“审阅”选项卡,然后select“ 取消保护表 ”并input密码即可。

你真的想避免通过VBA设置密码,因为进入密码很容易find密码。

以下是基本知识,您可以添加无效密码的检查。

 Private Sub Worksheet_Activate() Dim Msg As String Dim UserEntry As String Msg = InputBox("What is the password?") If Msg = "test" Then ActiveSheet.Unprotect ("password") End If End Sub 

在ThisWorkbook模块中试试这个

  Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Range) On Error GoTo WrongPassword Sh.Unprotect CleanUp: Exit Sub WrongPassword: MsgBox "Wrong Password" End Sub