Santis789
Santis789

Reputation: 9

Workbook to close if passwords were incorrect

In case a user enters a worng password I want the workbook to close down without saving. However, the code I'm trying does not work.

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

Upvotes: 0

Views: 179

Answers (1)

BigBen
BigBen

Reputation: 50162

You need to repeat the mypass <> for each String you're checking.

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

That's cumbersome. Use Select Case and Case Else to close.

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

Upvotes: 3

Related Questions