Excel密码保护表

时间:2017-03-28 08:28:33

标签: excel vba

我使用此代码来密码保护两页 出于一些奇怪的原因,我可以隐藏“Sheet1”而不是“Sheet2”,因为它总是可见的 行Sheets(MySheet2).Visible = True的原因是,如果有人输入错误的密码,它不会立即隐藏它。

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 个答案:

答案 0 :(得分: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代码窗格