prevent select sheet if the password is wrong by inputbox

abdelfattah

Well-known Member
Joined
May 3, 2019
Messages
1,429
Office Version
  1. 2019
  2. 2010
Platform
  1. Windows
hello I have about 10 sheets the first names is MAIN and the rests call( sh2,sh3... and so on )
so what I want when I select one of them to how the data it prevent select the sheet and return the sheet main and show inputbox to enter the password if is right then show what I select the sheet
note: if it's possible do that without hide and unhide the sheets
 

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
Say that you created a UserForm asking for password. If the UserForm is not in Modeless mode, as long as the UserForm is displayed you cannot select any sheet. In fact you cannot do anything until the UserForm is unloaded. Once unloaded, then you can select any sheet you like. Is this what you wanted?
 
Upvote 0
Hi
I mean by inputbox user not userform I search in the internet but the problem all use by hide and inside the sheets I don't want that , I know how do that by the userform
 
Upvote 0
Hi
I mean by inputbox user not userform I search in the internet but the problem all use by hide and inside the sheets I don't want that , I know how do that by the userform
Any reason not to use userform?
 
Upvote 0
actually no real reason but I prefer work inside the sheet not by userform creating form cause slow in my file but by inputbox is better
This is my view
 
Upvote 0
Do you mean something like this ?

Code in the ThisWorkbook Module

VBA Code:
Private Sub Workbook_SheetDeactivate(ByVal Sh As Object)

    Const PASSWORD = "1234"
    Dim sPassword As String
    Dim oCurSheet As Worksheet
   
    On Error GoTo errHandler
   
    Application.EnableEvents = False
        Set oCurSheet = ActiveSheet
        Sheets("MAIN").Activate
        sPassword = InputBox("Enter sheet password")
        If sPassword = PASSWORD Then
            oCurSheet.Activate
        Else
            MsgBox "wrong password"
        End If
errHandler:
    Application.EnableEvents = True

End Sub

EDIT: this assumes all worksheets share the same password. The code will need some tweaking if the sheets have a different password each.
 
Upvote 0
actually no real reason but I prefer work inside the sheet not by userform creating form cause slow in my file but by inputbox is better
This is my view
I think InputBox is also behaving like modal userform. You cannot do anything if the InpuBox still displayed, right? This is an easy sample which I think that will perform what you wanted. This will ask password whenever you select different sheet other than MAIN

Code is in ThisWorkbook
VBA Code:
Public PassOK As Boolean

Private Sub Workbook_Open()
PassOK = False
End Sub

Private Sub Workbook_SheetActivate(ByVal Sh As Object)

If Not Sh.Name = "MAIN" Then
    If Not PassOK Then Sheets("MAIN").Select
Again:
    ans = InputBox("Enter Password")
    If ans = "123" Then
        PassOK = True
        MsgBox "Password Accepted"
        Application.EnableEvents = False
        Sh.Select
        Application.EnableEvents = True
        PassOK = False
    Else
        MsgBox "Wrong password"
        GoTo Again
    End If
End If

End Sub
 
Upvote 0
thanks guys two codes both works greatly ! :)
just curiosity as @Jaafar Tribak The code will need some tweaking if the sheets have a different password each.
how is that, please?
 
Upvote 0
thanks guys two codes both works greatly ! :)
just curiosity as @Jaafar Tribak The code will need some tweaking if the sheets have a different password each.
how is that, please?
You just need to set password in this line
Const PASSWORD = "1234"
 
Upvote 0
@Zot I don't want changing the main password I'M talking about if I select each sheet the password it 's different if I select sh2 the password should be "123 and if I select sh3 the password should be "111" and so on the rests of sheets
 
Upvote 0

Forum statistics

Threads
1,214,561
Messages
6,120,234
Members
448,951
Latest member
jennlynn

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top