Password to be unable to view worksheet

antony32

New Member
Joined
May 10, 2004
Messages
11
I am currently setting up a spreadsheet where i need one of the worksheets to be locked & unable to be viewed unless a password is inputted.

Is this at all possible within Excel?

For Example.

Sheet 1 = Staff member 1

Sheet 2 = Staff member 2

Sheet 3 = Staff member 3

Sheet 4 = Supervisor

I need Sheet 4 to be locked & unable to even be viewed unless a password is entered.
 
Please can someone run through & explain clearly how i set up 'XLVeryHidden', as you've hit the nail on the head. If the user chooses to disable macros, they can accesss all of the pages!

Many Thanks
 
Upvote 0

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.
To set up a sheet to be xlSheetVeryHidden, right-click on the sheet name at the bottom of the workbook. Click "View Code". Then, hit F4.

In the spot labelled "Visible," select xlSheetVeryHidden.

To undo this with code:

Sheets("Sheet 1").Visible = xlSheetVisible

or, click on the sheet name in your Visual Basic editor, and edit the properties again.
 
Upvote 0
Hin Antony,

if the sheet is hidden, how could the users activate it ??

You would still need some VBA code to unhide the sheet maybe via a Button or a commandbar control that the users can click on or by using the code I posted.

One solution is perhaps to set the xlveryHidden property as OdinsDream explains combined with the code I posted.

However if you choose this route, you will need to add this to the previous code:

Private Sub Workbook_Open()
Sheets("Sheet4").Visible = True
End Sub


Private Sub Workbook_BeforeClose(Cancel As Boolean)
Sheets("Sheet4").Visible = xlVeryHidden
End Sub


Now if the user disables the macros, Sheet4 will remain hidden otherwise it will be visible only if correct password is provided.

I hope this is not overkill.
 
Upvote 0
pending 2 things...

1. is this running on a network (presume so being a work thing)
2. how many people need access to sheet4?

if the answer to 1 is yes, and 2 is not variable - ie certain users have access...

easiest thing is to utilise the environment variables and extract the userid of the current user - note this is not the same as the application.username (ie the username assigned to the copy of XL being used) rather it is your login id for the network.

so you have a sheet (xlveryhidden) called login or something

in this sheet (col A) you have a list of those user ids allowed to view sheet 4

then simply check the userid when the file is opened to your list of approved users - if the user is allowed to view 4 then you unhide it, then as stated earlier re-xlveryhidden on close...

ex...

Code:
Private Sub Workbook_Open()

uid = environ("username")

On Error GoTo AccessDenied:
x = Application.WorksheetFunction.Match(uid,Sheets("login").Range("A:A"),0)

Sheets("Sheet4").Visible = True

AccessDenied:

End Sub

Code:
Private Sub Workbook_BeforeClose(Cancel As Boolean) 
Sheets("Sheet4").Visible = xlVeryHidden 
End Sub

So if VBA is disabled by default NO ONE can see Sheet4... if VBA enabled only approved users can see it - ie those whose user id appears in your list.
 
Upvote 0
lasw10 said:
pending 2 things...

1. is this running on a network (presume so being a work thing)
2. how many people need access to sheet4?

if the answer to 1 is yes, and 2 is not variable - ie certain users have access...

easiest thing is to utilise the environment variables and extract the userid of the current user - note this is not the same as the application.username (ie the username assigned to the copy of XL being used) rather it is your login id for the network.

so you have a sheet (xlveryhidden) called login or something

in this sheet (col A) you have a list of those user ids allowed to view sheet 4

then simply check the userid when the file is opened to your list of approved users - if the user is allowed to view 4 then you unhide it, then as stated earlier re-xlveryhidden on close...


Laws10, That's definitely a much cleaner and elegant approach :)

I am not familiar with this VBA function, in particular the meaning of the environment variables. The online help being so slick :unsure:

Do you know any other useful uses of this Function ?

Regards.
 
Upvote 0
I'm not on a network presently but try this

Code:
Private Sub a()
Dim i As Integer

    i = 1
    Do
        If Environ(i) = "" Then Exit Do

        Cells(i, 1) = Environ(i)
        i = i + 1
    Loop


End Sub
 
Upvote 0
Thats great Jaafar, the code you've given does precisely what I required it to do.

What i'll now need to do now i've got the hang of it is to have the password protection on multiple sheets:

Example:

sheet1 - unlocked
sheet2 - unlocked
sheet3 - unlocked
sheet4 - password protected
sheet5 - unlocked
sheet6 - password protected
Sheet7 - password protected

What do i need to do to the code to enable this?

PS. if you're thinking i'm asking VERY simple questions, it'll be because i've been using VBA for about a day!
 
Upvote 0
Hi

Cant you just go to tools~protection then select sheet. This should then allow you to save the selected sheet rather than the workbook.

D :confused:
 
Upvote 0
Antony,

Below is the adapted code to accomodate the 3 Sheets as requested :

<font face=Courier New><SPAN style="color:#00007F">Dim</SPAN> LockedSheets <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Object</SPAN>
<SPAN style="color:#00007F">Dim</SPAN> LastActiveSheet <SPAN style="color:#00007F">As</SPAN> Worksheet
<SPAN style="color:#00007F">Dim</SPAN> S <SPAN style="color:#00007F">As</SPAN> Worksheet
<SPAN style="color:#00007F">Dim</SPAN> TempSheetVar <SPAN style="color:#00007F">As</SPAN> Worksheet
<SPAN style="color:#00007F">Dim</SPAN> UserInput <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Variant</SPAN>
<SPAN style="color:#00007F">Const</SPAN> PWord1 <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN> = "Enter Password1 Here"
<SPAN style="color:#00007F">Const</SPAN> PWord2 <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN> = "Enter Password2 Here"
<SPAN style="color:#00007F">Const</SPAN> Msg1 <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN> = "   Is Locked For Viewing !" & vbNewLine _
& vbNewLine & "Enter Password To Unlock."
<SPAN style="color:#00007F">Const</SPAN> Msg2 <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN> = "Wrong Password !"


<SPAN style="color:#00007F">Private</SPAN> <SPAN style="color:#00007F">Sub</SPAN> Workbook_BeforeClose(Cancel <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Boolean</SPAN>)
    <SPAN style="color:#00007F">On</SPAN> <SPAN style="color:#00007F">Error</SPAN> <SPAN style="color:#00007F">Resume</SPAN> <SPAN style="color:#00007F">Next</SPAN>
    Application.ScreenUpdating = <SPAN style="color:#00007F">False</SPAN>
    <SPAN style="color:#00007F">For</SPAN> <SPAN style="color:#00007F">Each</SPAN> S <SPAN style="color:#00007F">In</SPAN> LockedSheets
        S.Visible = xlvryhidden
    <SPAN style="color:#00007F">Next</SPAN> S
    Me.Save <SPAN style="color:#007F00">'This is to ensure hiding sheets is saved</SPAN>
<SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN>


<SPAN style="color:#00007F">Private</SPAN> <SPAN style="color:#00007F">Sub</SPAN> Workbook_Open()
    <SPAN style="color:#00007F">On</SPAN> <SPAN style="color:#00007F">Error</SPAN> <SPAN style="color:#00007F">Resume</SPAN> <SPAN style="color:#00007F">Next</SPAN>
    Application.ScreenUpdating = <SPAN style="color:#00007F">False</SPAN>
    <SPAN style="color:#00007F">Set</SPAN> LockedSheets = Worksheets(Array("Sheet4", "Sheet6", "Sheet7"))
    <SPAN style="color:#00007F">For</SPAN> <SPAN style="color:#00007F">Each</SPAN> S <SPAN style="color:#00007F">In</SPAN> LockedSheets
        S.Visible = <SPAN style="color:#00007F">True</SPAN>
    <SPAN style="color:#00007F">Next</SPAN> S
<SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN>


<SPAN style="color:#00007F">Private</SPAN> <SPAN style="color:#00007F">Sub</SPAN> Workbook_SheetDeactivate(<SPAN style="color:#00007F">ByVal</SPAN> Sh <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Object</SPAN>)
    <SPAN style="color:#00007F">On</SPAN> <SPAN style="color:#00007F">Error</SPAN> <SPAN style="color:#00007F">Resume</SPAN> <SPAN style="color:#00007F">Next</SPAN>
    <SPAN style="color:#00007F">Set</SPAN> TempSheetVar = LockedSheets(Sh.Name)
    <SPAN style="color:#00007F">Set</SPAN> TempSheetVar = <SPAN style="color:#00007F">Nothing</SPAN>
    <SPAN style="color:#00007F">If</SPAN> Err.Number = 9 <SPAN style="color:#00007F">Then</SPAN> <SPAN style="color:#007F00">' if Sh out of range store it in a global var</SPAN>
        Err.Clear
        Application.ScreenUpdating = <SPAN style="color:#00007F">False</SPAN>
        <SPAN style="color:#00007F">Set</SPAN> LastActiveSheet = Sh
    <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN>
<SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN>


<SPAN style="color:#00007F">Private</SPAN> <SPAN style="color:#00007F">Sub</SPAN> Workbook_SheetActivate(<SPAN style="color:#00007F">ByVal</SPAN> Sh <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Object</SPAN>)
    <SPAN style="color:#00007F">On</SPAN> <SPAN style="color:#00007F">Error</SPAN> <SPAN style="color:#00007F">Resume</SPAN> <SPAN style="color:#00007F">Next</SPAN>
    <SPAN style="color:#00007F">Set</SPAN> TempSheetVar = LockedSheets(Sh.Name)
    <SPAN style="color:#00007F">If</SPAN> Err.Number = 0 <SPAN style="color:#00007F">Then</SPAN>
        <SPAN style="color:#00007F">With</SPAN> Application
            .EnableEvents = <SPAN style="color:#00007F">False</SPAN>
            Sh.Visible = <SPAN style="color:#00007F">False</SPAN>
            LastActiveSheet.Activate
            .EnableEvents = <SPAN style="color:#00007F">True</SPAN>
        <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">With</SPAN>
        PromptForPassword TempSheetVar
        <SPAN style="color:#00007F">Set</SPAN> TempSheetVar = <SPAN style="color:#00007F">Nothing</SPAN>
    <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN>
    Err.Clear
<SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN>


<SPAN style="color:#00007F">Sub</SPAN> PromptForPassword(LockedWorkSheet <SPAN style="color:#00007F">As</SPAN> Worksheet)
    <SPAN style="color:#00007F">With</SPAN> Application
        <SPAN style="color:#00007F">Do</SPAN>
            UserInput = .InputBox("'" & LockedWorkSheet.Name & "'" & Msg1)
            <SPAN style="color:#00007F">Select</SPAN> <SPAN style="color:#00007F">Case</SPAN> UserInput
                <SPAN style="color:#00007F">Case</SPAN> <SPAN style="color:#00007F">Is</SPAN> = <SPAN style="color:#00007F">False</SPAN> <SPAN style="color:#007F00">' if user cancells don't activate sheet</SPAN>
                    <SPAN style="color:#00007F">Exit</SPAN> <SPAN style="color:#00007F">Do</SPAN>
                <SPAN style="color:#00007F">Case</SPAN> <SPAN style="color:#00007F">Is</SPAN> = PWord1, PWord2 <SPAN style="color:#007F00">'  if password correct activate sheet4</SPAN>
                    <SPAN style="color:#00007F">Set</SPAN> LastActiveSheet = LockedWorkSheet
                    <SPAN style="color:#00007F">Exit</SPAN> <SPAN style="color:#00007F">Do</SPAN>
                <SPAN style="color:#00007F">Case</SPAN> <SPAN style="color:#00007F">Else</SPAN> <SPAN style="color:#007F00">'if wrong password give user another try</SPAN>
                    UserDecision = MsgBox(Msg2, vbRetryCancel): Beep
                <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Select</SPAN>
        <SPAN style="color:#00007F">Loop</SPAN> <SPAN style="color:#00007F">Until</SPAN> UserDecision = vbCancel
        LockedWorkSheet.Visible = <SPAN style="color:#00007F">True</SPAN>
        .EnableEvents = <SPAN style="color:#00007F">False</SPAN>
        LastActiveSheet.Activate
        .EnableEvents = <SPAN style="color:#00007F">True</SPAN>
    <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">With</SPAN>
<SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN>
</FONT>



Just paste this bunch of code on ThisWorkbook Module and close the workbook to save the changes.

Now when you reopen it, the code should come to live.

Note that if the user disables the Macros, the locked sheets remain Hidden.


Regards.
 
Upvote 0
What if I need each spreadsheet to be accessible by a 'master' password (one that will open any of the pages) & individual passwords for each page?

ie.

sheet1 - password abc OR xyz

sheet2 - password def OR xyz

sheet3 - password ghi OR xyz

I will have about 10 sheets within each database. If you could give me an example plus explain how to add extra sheets into it without having to find out the whole code, it'll be a great help!
 
Upvote 0

Forum statistics

Threads
1,215,772
Messages
6,126,812
Members
449,339
Latest member
Cap N

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