【问题标题】:Userform Password unhide worksheet用户表单密码取消隐藏工作表
【发布时间】:2017-09-03 08:48:59
【问题描述】:

我有一个名为(“日期”)的工作表,我希望隐藏此工作表,并且只能通过密码才能看到。 Application.ActiveSheet.Visible = 假/真。

我有一个用户表单设置。下面是我的表单后面的代码。

Private passwordStatus As Boolean

Private Sub CommandButton1_Click()
    Dim a As String
    Dim Password As String

    a = "123"
    Password = TextBox1.Text
    'Set Pawwordstatus at False before Testing
    passwordStatus = False
    If Password = a Then
        MsgBox "Password Correct.", vbInformation
        passwordStatus = True
        Unload Me
    Else
        MsgBox "Password Incorrect. Please try again.", vbCritical
    End If
End Sub

Function checkPassword() As Boolean
  UserForm1.Show
  'Shows the User Form. And after Closing the Form
  'The PasswordStatus Value will be returned and you can check if
  'it is true
  checkPassword = passwordStatus
End Function

问题:我不确定在我的工作表事件后面写什么代码,每次用户尝试访问此工作表时,都会显示用户表单并要求访问密码。

我在 thisworkbook 后面有这段代码:

Private Sub Workbook_BeforeClose(Cancel As Boolean)

    Worksheets("Dates").Visible = False

    'must save, if not save, it is not effect.
    Me.Save

End Sub

【问题讨论】:

    标签: vba excel


    【解决方案1】:

    在保存事件之前使用工作簿将给定工作表的可见属性设置为 xlVeryHidden 并在工作簿打开事件中显示您的密码表单,如果密码正确,请取消隐藏工作表。

    这样,当您保存文件时它会被隐藏,并且仅对打开密码的用户可见。

    【讨论】:

      【解决方案2】:

      首先,在标准Module 中声明公共变量

      Public LastActiveSht As Worksheet
      Public IsPassword As Boolean
      

      然后在ThisWorkBook模块中添加

      Private Sub Workbook_SheetDeactivate(ByVal Sh As Object)
          Set LastActiveSht = Sh
      End Sub
      
      Sub Workbook_SheetActivate(ByVal Sh As Object)
          If Sh.Name = "Dates" Then
              LastActiveSht.Activate
              Application.EnableEvents = False
              IsPassword = False
              UserForm1.Show
              Application.EnableEvents = True
          End If
      End Sub
      

      UserForm1CommandButton1_Click 似乎还可以,但我已将其更改为

      Private Sub CommandButton1_Click()
          Dim a As String
      
          a = "aaa"
          Password = TextBox1.Text
          If Password = a Then
              MsgBox "Password Correct.", vbInformation
              IsPassword = True
              Unload Me
          Else
              MsgBox "Password Incorrect. Please try again.", vbCritical
          End If
      End Sub
      

      现在,当用户点击 UserForm1 的CLOSE 按钮时,在USERFORM 模块中添加以下代码

      Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
          If IsPassword Then LstSht.Activate
      End Sub
      

      注意: 使用上述方法,您不必隐藏工作表或使其不可见,但每次选择工作表 Dates 时,都必须输入正确的密码.

      【讨论】:

      • 非常感谢。我在此工作簿中的 LstSht.Activate 上收到“需要对象”的错误。
      • @James - 哎呀!我的错。 LstSht.Activate 应该是 LastActiveSht.Activate
      【解决方案3】:

      这是密码表单的调用。将其安装在您拥有按钮(ActiveX 控件)的工作表的代码表上。它会对 CommandButton1 的点击做出反应(我会给它一个有意义的名称,例如 CmdPassword

      Private Sub CommandButton1_Click()
      
          Dim PwForm As UserForm1
          Dim a As String
          Dim Password As String
      
          Set PwForm = New UserForm1
          With PwForm
              .Tag = Password
              .Show
              If .Tag = 1 Then
                  ' show & activate (select) the hidden sheet
              End If
          End With
          Unload PwForm
          Set PwForm = Nothing
      End Sub
      

      此代码调用下面给出的函数Password。将其安装在与上述过程相同的代码表上。

      Private Function Password() As String
          ' 03 Sep 2017
      
          Dim Fun As String
          Dim PwArr As Variant
          Dim i As Long
      
          PwArr = Array(80, 97, 115, 115, 119, 111, 114, 100)
          For i = 0 To UBound(PwArr)
              Fun = Fun & Chr(PwArr(i))
          Next i
          Password = Fun
      End Function
      

      您看到了伪装密码的轻微尝试。该数组由子CallCreatePassword 创建。这和它调用的函数CreatePassword 不需要是您项目的一部分。您可以将此代码保留在其他地方。不管它在哪里,它都应该在一个标准代码模块上,它调用的函数在它下面。

      Private Sub CallCreatePassword()
          ' 03 Sep 2017
      
          ' ===================================================
          ' Use this sub to call the CreatePassword sub
          ' which will print a string of numbers to the Immediate window.
          ' Paste that string into the PwArr array in the function Password
          ' ===================================================
      
          CreatePassword ("Password")             ' enter the password of your choice
      End Sub
      
      Private Sub CreatePassword(ByVal Pw As String)
          ' 03 Sep 2017
      
          Dim Fun() As String
          Dim i As Integer
      
          ReDim Fun(0 To Len(Pw) - 1)
          For i = 1 To Len(Pw)
              Fun(i - 1) = Asc(Mid(Pw, i, 1))
          Next i
          Debug.Print Join(Fun, " ,")
      End Sub
      

      回到 Click_procedure。在显示该表单之前,密码将写入表单的Tag 属性。使用Show 命令将控制传递给用户窗体。只有在来自用户窗体的Hide 命令之后,此过程中的代码才会继续运行。

      用户窗体应该有两个按钮(不仅仅是一个像你的按钮)。两者都包含Hide 命令,但它们将Tag 属性设置为1 或0。这两个程序都必须安装在 Userform1 的代码表上。

      Private Sub CmdCancel_Click()
      
          Tag = ""
          Hide
      End Sub
      
      Private Sub CmdOK_Click()
          ' 03 Sep 2017
      
          With TextBox1
              If .Text = Tag Then
                  Tag = 1
                  Hide
              Else
                  MsgBox "This password is not correct." & vbCr & _
                         "Press ""Cancel"" to exit."
                  .Text = ""
                  .SetFocus
              End If
          End With
      End Sub
      

      表单隐藏后,点击过程继续进行。如果标记的值 = 1(实际上是一个字符串),隐藏的工作表将变为可见并激活。否则它不会。无论哪种情况,程序都会结束。

      您可能希望添加一个触发 Before_Close 的事件过程以再次使工作表非常隐藏。

      【讨论】:

      • 感谢上面的代码。这整个代码是否落后于我的用户表单?
      • 我在以下代码上遇到错误:自动化错误:被调用者(服务器{非服务器应用程序})不可用并消失;所有连接都无效。调用可能已执行。 "如果 .Tag = 1 那么"
      • 我在上面的答案中添加了安装说明。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-07-05
      • 2023-03-27
      • 2011-02-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多