0

我在一张 Excel 表上有一个命令按钮,用于隐藏/取消隐藏其中一张工作表。请参见下面的代码。

但是,现在我想添加管理员输入密码以取消隐藏工作表的功能,否则任何人都可以单击按钮并取消隐藏数据表。

有没有办法强制excel显示正常的输入密码提示取消隐藏?

谢谢

Sub myButton()

ActiveWorkbook.Unprotect

If (Sheets("Sheet2").Visible) Then 
    Sheets("Sheet2").Visible = False 
    Sheets("Sheet1").Select          
Else
    Sheets("Sheet2").Visible = True  
    Sheets("Sheet2").Select          
End If

ActiveWorkbook.Protect Password:="password", structure:=True, Windows:=False

End Sub
4

2 回答 2

0

您可以使用输入框来收集用户的输入。不完全确定您要做什么,但也许这样的事情有效。

Option Explicit
Sub myButton()

    Dim pword As String

    pword = InputBox("Please enter a password", "Password Entry")

    If (Len(Trim$(pword)) = 0) Then Exit Sub

    ActiveWorkbook.Unprotect pword

    If (Sheets("Sheet2").Visible) Then
        Sheets("Sheet2").Visible = False
        Sheets("Sheet1").Select
    Else
        Sheets("Sheet2").Visible = True
        Sheets("Sheet2").Select
    End If

    ActiveWorkbook.Protect Password:=pword, structure:=True, Windows:=False

End Sub
于 2019-08-28T23:28:38.357 回答
0

我能够通过一些调整来解决这个问题。感谢瑞恩的帮助!

Sub myButton()

Dim pword As String

If (Sheets("Sheet2").Visible) Then
    Sheets("Sheet2").Visible = False
    Sheets("Sheet1").Select
Else

pword = InputBox("Please enter a password", "Password Entry")

If pword = "password" Then

ActiveWorkbook.Unprotect "password"

    Sheets("Sheet2").Visible = True
    Sheets("Sheet2").Select

ActiveWorkbook.Protect Password:="password", structure:=True, Windows:=False

Else
    MsgBox ("Wrong Password")
End If
End If

结束子

于 2019-08-29T15:41:04.923 回答