【问题标题】:Access to Excel worksheet with a password使用密码访问 Excel 工作表
【发布时间】:2019-07-10 18:38:05
【问题描述】:

我创建了一个包含 3 个工作表的 Excel 文件,如下所示:

我的目标是:使工作表只能通过密码访问。这意味着您只能通过密码查看工作表的内容。 例如:当“用户”点击“管理员”时,只有输入正确的密码才能看到工作表的内容。

工作表保护没用。

有可能吗?

【问题讨论】:

    标签: excel vba ms-access worksheet


    【解决方案1】:

    不可能安全地保护一张纸不被查看。您只能保护整个工作簿不被查看(使用密码)。

    您尝试使用密码安全隐藏/保护工作表的任何解决方法都可以很容易地被任何用户欺骗。

    安全地向用户隐藏数据的唯一方法是根本不分发这些数据。唯一真正安全的方法是拥有类似于客户端服务器进程的东西,其中服务器拥有原始数据,客户端向该服务器发送请求,而服务器只发送允许用户查看的数据。

    【讨论】:

      【解决方案2】:

      尝试以下代码。

      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标签并按住鼠标按钮而不松开它,您仍然可以看到工作表的内容;)
      【解决方案3】:

      这个 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
      

      【讨论】:

      • 请注意,“Access”(和标签)一词是用于访问数据而不是“Microsoft Access”程序。这个问题实际上是一个纯 Excel 问题。
      猜你喜欢
      • 1970-01-01
      • 2019-05-27
      • 1970-01-01
      • 1970-01-01
      • 2012-04-23
      • 2016-05-09
      • 2020-06-04
      • 2011-03-03
      • 1970-01-01
      相关资源
      最近更新 更多