2017-09-03 52 views
1

我有名为(“日期”)的工作表,我希望这个工作表被隐藏,只有通过密码才可见。 Application.ActiveSheet.Visible = False/True。Userform密码取消隐藏工作表

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

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 

回答

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_ClickUserForm1似乎不错,但我已经改变了一点作为

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被选中,必须输入正确的密码。

+0

非常感谢。我在本书中的LstSht.Activate中收到了“需要的对象”的错误。 – James

+0

@詹姆斯 - 欧帕!我的错。 'LstSht.Activate'应该是'LastActiveSht.Activate' – Mrig

+0

再次感谢你一个了不起的工作 – James

1

使用工作簿之前保存事件设置指定的工作表xlVeryHidden在打开工作簿事件的Visible属性表明您的密码形式,如果密码是正确的,取消隐藏表格。

这种方式在保存文件时会被隐藏,并且只有打开密码的用户才能看到。

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命令控件传递给用户窗体。此proc中的代码将仅在UserForm中的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以再次使工作表VeryHidden。

+0

感谢上面的代码。这整个代码是否落后于我的用户表单? – James

+0

我对此代码出现错误:自动化错误:被调用者(服务器{非服务器应用程序})无法使用并消失;所有连接都无效。该呼叫可能已执行。 “如果.Tag = 1然后” – James

+0

我已经将安装说明添加到我的上述答案。 – Variatus