Excel VBA Course
Excel VBA Course - From Beginner to Expert

200+ Video Lessons
50+ Hours of Video
200+ Excel Guides

Become a master of VBA and Macros in Excel and learn how to automate all of your tasks in Excel with this online course. (No VBA experience required.)

View Course

"compile error" show sheet based on entering password by input box

0

hello 

I  need  help  to  fix  the  error  , what  I want  if  I  select  sheet <> stock  then  show  inputbox  and  enter  the  password  to  show  the  sheet ,  but  it  gives error  compile erro in this  line 

If Application.InputBox("Enter Password", "Password") & LT;&GT; "airplane" Then

this  is  the code 

Private Sub Workbook_SheetActivate(ByVal Sh As Object)

Dim Num As Integer

If Pwd = "" Then
If Sh.Name <> "stock" Then
Num = ActiveWindow.Index
Windows(Num).Visible = False
If Application.InputBox("Enter Password", "Password") & LT;&GT; "airplane" Then
MsgBox "Incorrect Password", vbCritical, "Error"
Application.EnableEvents = False
Sheets(PvSh).Select
Application.EnableEvents = True
Else
Pwd = "airplane"
End If
Windows(Num).Visible = True
End If
End If
End Sub
Private Sub Workbook_SheetDeactivate(ByVal Sh As Object)
PvSh = Sh.Name
End Sub
Answer
Discuss

Answers

0
Selected Answer

Kalil

InputBox returns a string or a null string but your (incorrect and uncompiled) IF test is expecting a TRUE return (if we forget whatever undefined LT&GT are meant to be).

Try replacing your code with the code below, noting I've declared variables at the workbook level. It will keep the user on the STOCK sheet until the password entered. Key changes are in bold: 

Option Explicit
Dim PvSh As String, Pwd As String

Private Sub Workbook_SheetActivate(ByVal Sh As Object)

Dim Num As Integer

If Pwd = "" Then
    If Sh.Name <> "STOCK" Then
        Num = ActiveWindow.Index
        Windows(Num).Visible = False
        If InputBox("Enter Password", "Password", "????") <> "airplane" Then
            MsgBox "Incorrect Password", vbCritical, "Error"
            Application.EnableEvents = False
                Windows(Num).Visible = True
                Sheets("STOCK").Select
            Application.EnableEvents = True

            Else
            Pwd = "airplane"
        End If
        Windows(Num).Visible = True
    End If
End If
End Sub


Private Sub Workbook_SheetDeactivate(ByVal Sh As Object)

PvSh = Sh.Name

End Sub
You should also consider the alternative approach of hiding all other  worksheets until the correct password in entered- see Don's tutorial Require a Password to View Hidden Worksheets in Excel - VBA Tutorial

Hope this helps.

Discuss

Discussion

John 
many  thanks  for  your answering 
Kalil (rep: 36) Jun 6, '21 at 9:41 am
Glad it worked for you, Kalil. Thank you for selecting my answer
John_Ru (rep: 6152) Jun 6, '21 at 9:43 am
BTW if the answer code does what you want, you don't really need the Workbook_SheetDeactivate code (or to declare the variable PvSh unless you have use for it later)
John_Ru (rep: 6152) Jun 6, '21 at 9:52 am
I  note  after  enter   password  and  select  specific  sheet  and  save  and  close  then  when open  again  the  sheet  active is  what  I  select  last  sheet  before close the file    I  don't  want  the  user  see any  sheet  except  the  sheet STOCK  so  it  should  when save  and  close  and  open  always  activate  the  sheet  STOCK  where  I  put  this  line  sheets("stock").activate ,  am I right  or  wrong  please guide me ?
Kalil (rep: 36) Jun 6, '21 at 10:17 am
Kalil. That's a separate question really but there's an easy fix. Use the Workbook_Open event to always open it on that sheet:
Private Sub Workbook_Open()
Sheets("STOCK").Select
End Sub
Just paste that code with the other code, at the Workbook level.
John_Ru (rep: 6152) Jun 6, '21 at 10:23 am
John  sorry if  I  ask  a different  question   
thanks  for  your cooperation
Kalil (rep: 36) Jun 6, '21 at 10:40 am
No problem Kalil but it helps if your question says what problem you encountered AND what you're trying to do. Hope that simple Workbook_Open code worked for you.
John_Ru (rep: 6152) Jun 6, '21 at 10:47 am
Add to Discussion


Answer the Question

You must create an account to use the forum. Create an Account or Login