【发布时间】:2019-07-10 18:38:05
【问题描述】:
我创建了一个包含 3 个工作表的 Excel 文件,如下所示:
我的目标是:使工作表只能通过密码访问。这意味着您只能通过密码查看工作表的内容。 例如:当“用户”点击“管理员”时,只有输入正确的密码才能看到工作表的内容。
工作表保护没用。
有可能吗?
【问题讨论】:
标签: excel vba ms-access worksheet
我创建了一个包含 3 个工作表的 Excel 文件,如下所示:
我的目标是:使工作表只能通过密码访问。这意味着您只能通过密码查看工作表的内容。 例如:当“用户”点击“管理员”时,只有输入正确的密码才能看到工作表的内容。
工作表保护没用。
有可能吗?
【问题讨论】:
标签: excel vba ms-access worksheet
不可能安全地保护一张纸不被查看。您只能保护整个工作簿不被查看(使用密码)。
您尝试使用密码安全隐藏/保护工作表的任何解决方法都可以很容易地被任何用户欺骗。
安全地向用户隐藏数据的唯一方法是根本不分发这些数据。唯一真正安全的方法是拥有类似于客户端服务器进程的东西,其中服务器拥有原始数据,客户端向该服务器发送请求,而服务器只发送允许用户查看的数据。
【讨论】:
尝试以下代码。
Private Sub Workbook_SheetActivate(ByVal Sh As Object)
Dim MySheetName As String
MySheetName = "Admin1" 'The sheed name which you want to hide.
If Application.ActiveSheet.Name = MySheetName Then
Application.EnableEvents = False
Application.ActiveSheet.Visible = False
response = Application.InputBox("Password", "Enter Password", , Type:=2)
If response = "rainy2019" Then 'Unhide Password.
Application.Sheets(MySheetName).Visible = True
Application.Sheets(MySheetName).Select
End If
End If
Application.Sheets(MySheetName).Visible = True
Application.EnableEvents = True
End Sub
VBA 窗口:
【讨论】:
Admin1标签并按住鼠标按钮而不松开它,您仍然可以看到工作表的内容;)
这个 ADO 解决方案怎么样?!
添加参考:Microsoft ActiveX 数据对象 2.8 库
Sub test()
Dim Conn As New ADODB.Connection
Dim rs As New ADODB.Recordset
Dim dbPath As String
Dim aQuery As String
Dim pword As String
Dim strcon As String
dbPath = ThisWorkbook.Path & "\Database.mdb"
pword = "abcd"
aQuery = "SELECT * FROM myTable"
strcon = "Provider=Microsoft.Jet.OLEDB.4.0;" _
& "Data Source=" & dbPath & ";" _
& "Jet OLEDB:Database Password=" & pword & ";"
Conn.Open strcon
rs.Open aQuery, Conn
If Not (rs.EOF And rs.BOF) Then
MsgBox rs.Fields(0)
End If
rs.Close
Set rs = Nothing
Set Conn = Nothing
End Sub
或者,使用 DAO 解决方案。
添加参考:Microsoft DAO 3.6 对象库
Sub test()
Dim Conn As New ADODB.Connection
Dim rs As New ADODB.Recordset
Dim dbPath As String
Dim aQuery As String
Dim pword As String
Dim strcon As String
dbPath = ThisWorkbook.Path & "\Database.mdb"
pword = "abcd"
aQuery = "SELECT * FROM myTable"
strcon = "Provider=Microsoft.Jet.OLEDB.4.0;" _
& "Data Source=" & dbPath & ";" _
& "Jet OLEDB:Database Password=" & pword & ";"
Conn.Open strcon
rs.Open aQuery, Conn
If Not (rs.EOF And rs.BOF) Then
MsgBox rs.Fields(0)
End If
rs.Close
Set rs = Nothing
Set Conn = Nothing
End Sub
【讨论】: