用密码保护一张工作表

Excel中是否有内置的方式来保护工作簿中的一个电子表格,并使用密码?

就像用户勾选或select一个控件时,在Worksheet变为可见之前,会提示input密码。

如果它没有内置到Excel中,可以用VBA实现吗?

你可以尝试这种方法:

  • 使工作表VeryHiddenProtected ,使其无法从标准xl菜单中取消保护或检测到
  • Workbook_BeforeCloseWorkbook_Open事件中添加此保护
  • 在这个例子中,我已经使用了名为YourSheet的工作表
  • 你应该在这个项目中保护VBA以增加安全性。

插入一个Active Xcheckbox并使用代码检查是否:

  1. 该checkbox为True
  2. 用户知道密码来UnHide表。 (在这个例子中使用Fred

CheckBox代码

 Private Sub CheckBox1_Click() Dim StrPass As String If CheckBox1.Value Then StrPass = Application.InputBox("Please enter the password", "Admin check") If StrPass = "fred" Then With ThisWorkbook.Sheets("YourSheet") .Unprotect "fred" .Visible = xlSheetVisible MsgBox "Sheet unhidden!", vbInformation End With Else MsgBox "wrong password", vbCritical End If End If End Sub 

ThisWorkbook模块

 Private Sub Workbook_BeforeClose(Cancel As Boolean) With ThisWorkbook.Sheets("YourSheet") .Protect "fred" .Visible = xlVeryHidden End With End Sub Private Sub Workbook_Open() With ThisWorkbook.Sheets("YourSheet") .Protect "fred" .Visible = xlVeryHidden End With End Sub