Userform密码取消隐藏工作表

时间:2017-09-03 08:48:59

标签: vba excel-vba excel

我有一个名为(" Dates")的工作表,我希望隐藏这个工作表,只能通过密码看到。 Application.ActiveSheet.Visible = False / True。

我有一个用户表单设置。以下是我在表格背后的代码。

enter image description here

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

3 个答案:

答案 0 :(得分:1)

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

这样,当您保存文件时它会被隐藏,只有在打开密码的情况下才会对用户可见。

答案 1 :(得分:0)

这是密码表单的调用。将其安装在您具有按钮(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命令将控件传递给Userform。只有来自UserForm的Hide命令后,此proc中的代码才会继续运行。

userform应该有两个按钮(不只是一个像你的按钮)。两者都包含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

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

您可能希望添加一个事件过程,该过程触发Before_Close以使工作表再次出现。

答案 2 :(得分:0)

首先,在标准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

CommandButton1_Click的{​​{1}}似乎还可以,但我已将其更改为

UserForm1

现在,要处理用户点击UserForm1的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 按钮的时间,请在CLOSE模块中添加以下代码

USERFORM

注意: 使用上面的内容,您无需隐藏工作表或使其不可见,但每次选择工作表Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer) If IsPassword Then LstSht.Activate End Sub 时,都必须输入正确的密码进入。