VBA自定义密码可保护1张以上

时间:2018-11-20 16:50:23

标签: excel vba excel-vba

我目前正在使用启用了宏的Excel工作表,该工作表具有多个标签(我希望在9个标签上进行此操作,但出于问题的考虑,我将仅包括2个),并且每个标签我想添加一个与我在代码中指定的密码匹配的密码提示。

这可以正常工作,但是我的问题是,当在实际工作表选项卡上将两张表并排放置时,它将经过这两个表,而不是隐藏第一个表,直到我输入正确的密码为止。

例如,在我的工作表上,我有一个名为Cascada的选项卡,然后是一个名为Cascada2的选项卡。如果我在这两者之间放置一个空白标签,那么我的代码将正常工作。但是,当它们按顺序排列时,无论我是否输入正确的字符串,它似乎都会经历密码提示的顺序。

请参见下面的代码,我们将不胜感激。

谢谢。

使用答案进行了编辑更新

Private Sub Workbook_SheetActivate(ByVal Sh As Object)

Application.EnableEvents = False

Dim cascada As String, cascada2 As String
cascada = "Config_Cascada"
Rhea = "Config_Rhea"

Select Case Sh.Name
    Case cascada, cascada2

        Dim pwd As String
        pwd = "cascada" & IIf(Sh.Name = cascada2, 2, "")

        Dim Response As String
        Response = InputBox("Enter password to view sheet")

        If Response = pwd Then
            Sh.Select
        Else
        Worksheets("Doors").Activate
        End If


End Select

Select Case Sh.Name
    Case Rhea

        Dim pwdRhea As String
        pwdRhea = "rhea"

        Dim ResponseRhea As String
        ResponseRhea = InputBox("Enter password to view sheet")

        If Response = pwdRhea Then
            Sh.Select
        Else
        Worksheets("Doors").Activate
        End If


End Select

Application.EnableEvents = True

End Sub

1 个答案:

答案 0 :(得分:2)

试一下。清洁和工作,据我测试:

Private Sub Workbook_SheetActivate(ByVal Sh As Object)

    Application.EnableEvents = False

    Dim cascada As String, cascada2 As String
    cascada = "config_Cascada"
    cascada2 = "config_Cascada2"

    Select Case Sh.Name
        Case cascada, cascada2

            Dim pwd As String
            pwd = "cascada" & IIf(Sh.Name = cascada2, 2, "")

            Dim Response as String
            Response = InputBox("Enter password to view sheet")

            If Response = pwd Then
                Sh.Select
            End If

    End Select

    Application.EnableEvents = True

 End Sub