2017-09-03 53 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