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

prevent select sheet if password is wrong by inputbox

1

hi experts 

I try   to  prevent  the  user  from select sheet1   if  the  password   is  wrong  or  empty  by inputbox   ,    it shouldn't  select   and  stay  the  sheet2 is  activated    until  the  password is  right  ,  but  I  don't  successes so  far   so  please  guide  me   the  right  way 

this  is   what  i  have 

Private Sub Workbook_Open()
 Const pwd As String = "1234"
    Dim myValue As Variant

     Sheets("sheet2").Activate
   If Sheets("sheet1").Select Then
   Sheets("sheet2").Select
    myValue = InputBox("Enter Password [blank to abort]", "DCA Input Screen")
    Do While myValue <> pwd And myValue <> vbNullString
        MsgBox "Invalid Password", vbCritical
        Sheets("sheet2").Select
        myValue = InputBox("Enter Password [blank to abort]", "DCA Input Screen")
    Loop
    If myValue = vbNullString Then Exit Sub


    Sheets("sheet1").Select
    End If
End Sub

thanks in advance 

Answer
Discuss

Answers

2
Selected Answer

Please paste the event procedure below into the ThisWorkbook code module.

Option Explicit

Private Sub Workbook_Open()
    ' 198
    ThisWorkbook.Worksheets("TXL_5261").Activate
End Sub

In the workbook I created for testing I had a sheet called "TXL_5261". You can change the name to whatever suits you. The point is that it's a sheet that you are willing to show to all comers. Observe that this same sheet is also referred to in the code below. It's the one the user will be sent back to if access to the protected sheet is denied.

Paste the code below into the code sheet of the sheet that you want to protect.

Option Explicit

Private Sub Worksheet_Activate()
    ' 198

    Dim TopLeft         As Single           ' Top Left of a blank cell

    With ActiveSheet.UsedRange
        TopLeft = Cells(1, .Column + .Columns.Count + 1).Left
    End With
    With ActiveWindow
        .ScrollIntoView PointsToPixels(TopLeft), 0, _
                        PointsToPixels(.Width), _
                        PointsToPixels(.Height)
    End With

    If AllowAccess Then
        ActiveWindow.ScrollIntoView 0, 0, 0, 0
    Else
        Worksheets("TXL_5261").Activate
    End If
End Sub

Private Function AllowAccess() As Boolean
    ' 198

    Const Pwd       As String = "1234"
    Dim MyPwd       As Variant
    Dim Fun         As Boolean              ' Function return value

    Do
        MyPwd = InputBox("Enter Password [blank to abort]", "DCA Input Screen")
        If Len(MyPwd) = 0 Then Exit Do

        Fun = CBool(StrComp(MyPwd, Pwd, vbBinaryCompare))
        If Fun Then MsgBox "Invalid Password", vbCritical, "Access denied"
        Fun = Not Fun
    Loop Until Fun

    AllowAccess = Fun
End Function

Private Function PointsToPixels(ByVal Pts As Single) As Long
    '198
    PointsToPixels = Pts * 96 / 72
End Function

Want to know how it works? When the user clicks on the tab of Sheet2 (the name is immaterial because the code knows which sheet from the location of the code) Excel will display that sheet come what may. My code doesn't fight Excel. Instead, it moves the sheet's used range out of sight while the user enters the password. He sees an unused, blank area of the sheet.

From there it's simple. If the password is correct the view is moved to the beginning of the sheet. If access is denied the default worksheet is called up.

Note that you can't test the function AllowAccess while it's in the worksheet's code module. If you move it to a standard code module you can test it there with F5, and if you remove the scope limitation "Pravate" from the declaration you can call it from the Immediate pane.

Discuss

Discussion

@,Variatus. That's a really neat approach, I like it. 
John_Ru (rep: 6152) Mar 18, '21 at 2:15 am
Variatus 
it's very Brilliant !   I have  to  admit this  answer is  very  smart despite  I was wanting  prevent  showing  the  sheet which protected   until  writting the  right  password  but  this  answering  is  excellantly  
many  thanks
MAKLIL (rep: 34) Mar 18, '21 at 5:32 am
Add to Discussion
0

Maklil

The Workbook_Open sub isn't the place to do this but you could force the book to open on sheet 2 anyway by reducing it to just this:

Private Sub Workbook_Open()

     Sheets("sheet2").Activate

End Sub
While you could try to run a macro from the Workbook_SheetDeactivate event when a user moves from sheet2, they will see sheet 1 (though they can't edit until they successfully complete your input box etc.)

A better way is to hide sheet1 (or others) and require a password to even view that. See Don's tutorial Require a Password to View Hidden Worksheets in Excel - VBA Tutorial and modify to suit.

Discuss

Discussion

John
thank   you    about   the tutorial      I know  about   hide  the  sheet   but    I  search  another  different  way  without hide  the  sheet 
MAKLIL (rep: 34) Mar 17, '21 at 2:08 pm
Maklil
Understood but I have no more time to help you, sorry.
John_Ru (rep: 6152) Mar 17, '21 at 2:53 pm
John 
you  shouldn't  be  sorry   I  try   find  another  way  if  anybody  help me  is  ok  if  not  this  is  not  end  of  the  world    I will  trying  find  way 
thank  for  your  time 
MAKLIL (rep: 34) Mar 17, '21 at 3:07 pm
Maklil. Your post above should be in the Discussion under the answer from Variatus (or he won't be advised of it) AND you should deselect my Answer and Select the great answer from Variatus.
John_Ru (rep: 6152) Mar 18, '21 at 5:07 am
John 
sorry   and  thank  you for  inform  me   I corrected my select answer 
MAKLIL (rep: 34) Mar 18, '21 at 5:35 am
Maklil

I must say that your last comment " I was wanting  prevent  showing  the  sheet which protected   until  writting the  right  password " is a bit confusing since you'd previously rejected my suggestion to make the sheet hidden and reveal it after the password was entered (as per Don's tutorial). Oh well!
John_Ru (rep: 6152) Mar 18, '21 at 7:04 am
Add to Discussion


Answer the Question

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