如果密码不正确,工作簿将关闭

时间:2019-10-30 14:07:56

标签: excel vba

如果用户输入了破旧的密码,我希望工作簿在不保存的情况下关闭。但是,我正在尝试的代码无法正常工作。

Sub Unhide()

mypass = Application.InputBox( _
prompt:="Enter password - In case of wrong password please close the file 
and try again")


If mypass = "BJE" Then Sheets("Overview").Visible = True
If mypass = "BFL" Then Sheets("BFL").Visible = True
If mypass = "EBR" Then Sheets("EBR").Visible = True
If mypass = "DME" Then Sheets("DME").Visible = True
If mypass = "AJA" Then Sheets("AJA").Visible = True
If mypass = "RLC" Then Sheets("RLC").Visible = True
If mypass = "JMK" Then Sheets("JMK").Visible = True
If mypass = "AXB" Then Sheets("AXB").Visible = True
If mypass = "JIK" Then Sheets("JIK").Visible = True
If mypass = "KKO" Then Sheets("KKO").Visible = True

If mypass <> "BJE" Or "BFL" Or "EBR" Or "DME" Or "AJA" Or "RLC" Or "JMK" 
Or "AXB" Or "JIK" Or "KKO" Then ThisWorkbook.Close


End Sub

1 个答案:

答案 0 :(得分:3)

您需要为要检查的每个mypass <>重复String

If mypass <> "BJE" Or mypass <> "BFL" Or mypass <> "EBR"...

这很麻烦。使用Select CaseCase Else关闭。

Select Case myPass
    Case "BJE"
         ThisWorkbook.Sheets("Overview").Visible = True ' Or use the sheet code name
    Case "BFL"
         ThisWorkbook.Sheets("BFL").Visible = True

    ... ' and so on

    Case Else
         ThisWorkbook.Close ' though as pointed out, this is a pretty harsh user experience
End Select