2017-03-28 179 views
0

我使用此密碼來保護兩頁。
由於某些奇怪的原因,我可以隱藏「Sheet1」而不是「Sheet2」,因爲它總是可見的。
Sheets(MySheet2).Visible = True的原因是,如果有人輸入了錯誤的密碼,它不會立即隱藏它。Excel密碼保護表

Private Sub Workbook_SheetActivate(ByVal Sh As Object) 
Dim MySheets As String, Response As String 
MySheet = "Sheet1" 
MySheet2 ="Sheet2" 
If ActiveSheet.Name = MySheet Then 
ActiveSheet.Visible = False 
    Response = InputBox("Enter password to view sheet") 
     If Response = "MyPass" Then 
      Sheets(MySheet).Visible = True 
      Application.EnableEvents = False 
      Sheets(MySheet).Select 
      Application.EnableEvents = True 
     End If 
End If 
Sheets(MySheet).Visible = True 
Private Sub Workbook_SheetActivate(ByVal Sh As Object) 
If ActiveSheet.Name = MySheet2 Then 
ActiveSheet.Visible = False 
    Response = InputBox("Enter password to view sheet") 
     If Response = "MyPass" Then 
      Sheets(MySheet2).Visible = True 
      Application.EnableEvents = False 
      Sheets(MySheet2).Select 
      Application.EnableEvents = True 
     End If 
End If 
Sheets(MySheet2).Visible = True 
    End Sub 
+1

在那裏工作簿中只有兩個表?我沒有測試過你的代碼,但我會想象你正在試圖隱藏這兩個表單,從而導致錯誤。 – Miguel

回答

2

我想你是在此之後:

Private Sub Workbook_SheetActivate(ByVal Sh As Object) 
    Dim MySheets As String, Response As String 

    With ActiveSheet 
     Select Case .Name 
      Case "Sheet1", "Sheet2" 
       Application.EnableEvents = False 
       .Visible = False 
       Response = InputBox("Enter password to view sheet") 
       If Response = "MyPass" Then 
        .Visible = True 
        .Select 
       End If 
       Application.EnableEvents = True 
      End Select 
    End With 
End Sub 

,你應該已經知道了,這個代碼是被放置在ThisWorkbook代碼窗格

+0

這很有效一件事,如果有人輸入了錯誤的密碼,那麼工作表就隱藏起來了。然後無需手動取消隱藏它就無法訪問了 –

+2

以及那種情況下通緝行爲是什麼? – user3598756

+0

只是無法執行該表格,並且在再次嘗試時再次要求輸入密碼。 –