User name and password to open excel file

mezr

Active Member
Joined
Feb 2, 2003
Messages
301
I know that Excel files can be password protected, but is it possible to create multiple, unique user names and passwords that would allow only a select group of individuals to open an excel file?

Also, to take it even a step further, if the above is possible, then is it possible to cause the file to automatically create a running list of what day and time which user accessed the file?
It seems like a lot to ask, but I'm so often amazed by what I've learned on this board that I figured it was worth a shot!
 

Excel Facts

How to create a cell-sized chart?
Tiny charts, called Sparklines, were added to Excel 2010. Look for Sparklines on the Insert tab.
In order for this solution to work, you really should password protect your VB Project. Create a list of user names, passwords, and access to sheets like this:
zzzzzz.xls
ABCDE
1UserNamePasswordSheet1Sheet2Sheet3
2BillyDumb-buttxx
3TomMoldyxx
4JuanStinkyxxx
User List


Make this sheet xlSheetVeryHidden for it's Visible property. Create a worksheet called WELCOME SCREEN that will basically be a background or even just an all black page. Then try this code out:<font face=Courier New><SPAN style="color:darkblue">Private</SPAN><SPAN style="color:darkblue">Sub</SPAN> Workbook_Open()<SPAN style="color:darkblue">Dim</SPAN> Sh<SPAN style="color:darkblue">As</SPAN> Worksheet<SPAN style="color:darkblue">Dim</SPAN> UserName<SPAN style="color:darkblue">As</SPAN><SPAN style="color:darkblue">String</SPAN><SPAN style="color:darkblue">Dim</SPAN> Password<SPAN style="color:darkblue">As</SPAN><SPAN style="color:darkblue">String</SPAN><SPAN style="color:darkblue">Dim</SPAN> ThisCell<SPAN style="color:darkblue">As</SPAN> Range<SPAN style="color:darkblue">Dim</SPAN> c<SPAN style="color:darkblue">As</SPAN><SPAN style="color:darkblue">Long</SPAN>
    <SPAN style="color:darkblue">For</SPAN><SPAN style="color:darkblue">Each</SPAN> Sh<SPAN style="color:darkblue">In</SPAN> ThisWorkbook.Worksheets
        <SPAN style="color:darkblue">If</SPAN> Sh.Name<> "WELCOME SCREEN"<SPAN style="color:darkblue">Then</SPAN>
            Sh.Visible = xlSheetVeryHidden
        <SPAN style="color:darkblue">End</SPAN><SPAN style="color:darkblue">If</SPAN>
    <SPAN style="color:darkblue">Next</SPAN> Sh
    UserName = InputBox("Please enter your user name.")
    Password = InputBox("Please enter password.")
    <SPAN style="color:darkblue">For</SPAN><SPAN style="color:darkblue">Each</SPAN> ThisCell<SPAN style="color:darkblue">In</SPAN> Sheets("User List").Range("A2:A" & Sheets("User List").Range("A65536").End(xlUp).Row)
        <SPAN style="color:darkblue">If</SPAN> UCase(ThisCell.Value) = UCase(UserName) And UCase(ThisCell.Offset(, 1).Value) = UCase(Password)<SPAN style="color:darkblue">Then</SPAN>
            MsgBox "Access Granted"
            <SPAN style="color:darkblue">For</SPAN> c = 2<SPAN style="color:darkblue">To</SPAN> 4
            <SPAN style="color:green">'This is the number of sheets from C1 to E1</SPAN>
                <SPAN style="color:darkblue">If</SPAN> ThisCell.Offset(, c).Value<> ""<SPAN style="color:darkblue">Then</SPAN>
                    Sheets(Sheets("User List").Cells(1, c + 1).Value).Visible = xlSheetVisible
                <SPAN style="color:darkblue">End</SPAN><SPAN style="color:darkblue">If</SPAN>
            <SPAN style="color:darkblue">Next</SPAN> c
            <SPAN style="color:darkblue">Exit</SPAN><SPAN style="color:darkblue">Sub</SPAN>
        <SPAN style="color:darkblue">End</SPAN><SPAN style="color:darkblue">If</SPAN>
    <SPAN style="color:darkblue">Next</SPAN> ThisCell
    MsgBox "Access Denied"
    ThisWorkbook.<SPAN style="color:darkblue">Close</SPAN><SPAN style="color:darkblue">End</SPAN><SPAN style="color:darkblue">Sub</SPAN></FONT>
 
Upvote 1
You can then add something like:

Sheets("User List").Range("F" & ThisCell.Row).Value = Now

so that you can have the last date and time that user accessed the file.
 
Upvote 0
could you do the same visibility thing with just a cell not the whole worksheet. if yes please let me know what changes should i make in the code. Thanx and i dont' quiet get it. Shall i create a worksheet just for the users and their password or just type it in straight into the code
thanx again


phantom1975 said:
In order for this solution to work, you really should password protect your VB Project. Create a list of user names, passwords, and access to sheets like this:
zzzzzz.xls
ABCDE
1UserNamePasswordSheet1Sheet2Sheet3
2BillyDumb-buttxx
3TomMoldyxx
4JuanStinkyxxx
User List


Make this sheet xlSheetVeryHidden for it's Visible property. Create a worksheet called WELCOME SCREEN that will basically be a background or even just an all black page. Then try this code out:<font face=Courier New><SPAN style="color:darkblue">Private</SPAN><SPAN style="color:darkblue">Sub</SPAN> Workbook_Open()<SPAN style="color:darkblue">Dim</SPAN> Sh<SPAN style="color:darkblue">As</SPAN> Worksheet<SPAN style="color:darkblue">Dim</SPAN> UserName<SPAN style="color:darkblue">As</SPAN><SPAN style="color:darkblue">String</SPAN><SPAN style="color:darkblue">Dim</SPAN> Password<SPAN style="color:darkblue">As</SPAN><SPAN style="color:darkblue">String</SPAN><SPAN style="color:darkblue">Dim</SPAN> ThisCell<SPAN style="color:darkblue">As</SPAN> Range<SPAN style="color:darkblue">Dim</SPAN> c<SPAN style="color:darkblue">As</SPAN><SPAN style="color:darkblue">Long</SPAN>
    <SPAN style="color:darkblue">For</SPAN><SPAN style="color:darkblue">Each</SPAN> Sh<SPAN style="color:darkblue">In</SPAN> ThisWorkbook.Worksheets
        <SPAN style="color:darkblue">If</SPAN> Sh.Name<> "WELCOME SCREEN"<SPAN style="color:darkblue">Then</SPAN>
            Sh.Visible = xlSheetVeryHidden
        <SPAN style="color:darkblue">End</SPAN><SPAN style="color:darkblue">If</SPAN>
    <SPAN style="color:darkblue">Next</SPAN> Sh
    UserName = InputBox("Please enter your user name.")
    Password = InputBox("Please enter password.")
    <SPAN style="color:darkblue">For</SPAN><SPAN style="color:darkblue">Each</SPAN> ThisCell<SPAN style="color:darkblue">In</SPAN> Sheets("User List").Range("A2:A" & Sheets("User List").Range("A65536").End(xlUp).Row)
        <SPAN style="color:darkblue">If</SPAN> UCase(ThisCell.Value) = UCase(UserName) And UCase(ThisCell.Offset(, 1).Value) = UCase(Password)<SPAN style="color:darkblue">Then</SPAN>
            MsgBox "Access Granted"
            <SPAN style="color:darkblue">For</SPAN> c = 2<SPAN style="color:darkblue">To</SPAN> 4
            <SPAN style="color:green">'This is the number of sheets from C1 to E1</SPAN>
                <SPAN style="color:darkblue">If</SPAN> ThisCell.Offset(, c).Value<> ""<SPAN style="color:darkblue">Then</SPAN>
                    Sheets(Sheets("User List").Cells(1, c + 1).Value).Visible = xlSheetVisible
                <SPAN style="color:darkblue">End</SPAN><SPAN style="color:darkblue">If</SPAN>
            <SPAN style="color:darkblue">Next</SPAN> c
            <SPAN style="color:darkblue">Exit</SPAN><SPAN style="color:darkblue">Sub</SPAN>
        <SPAN style="color:darkblue">End</SPAN><SPAN style="color:darkblue">If</SPAN>
    <SPAN style="color:darkblue">Next</SPAN> ThisCell
    MsgBox "Access Denied"
    ThisWorkbook.<SPAN style="color:darkblue">Close</SPAN><SPAN style="color:darkblue">End</SPAN><SPAN style="color:darkblue">Sub</SPAN></FONT>
 
Upvote 0
could you do the same visibility thing with just a cell not the whole worksheet. if yes please let me know what changes should i make in the code. Thanx and i dont' quiet get it. Shall i create a worksheet just for the users and their password or just type it in straight into the code
Unfortunately, you can't hide just a single cell, but you do have several options: Hide the Row or Column, set that cell's Format--Protection to Hidden, or set its Font color to White. (Although with that method, if a user highlights a range of cells contiguous to that one, they'll see the cell's contents).

As for the second part, follow Phantom's example. It can be a bit if a pain in the butt to have to modify your code, finding individual user names and passwords.

Hope that helps,

Smitty
 
Upvote 0
or set its Font color to White
Another option for this bit of Smitty's suggestion is a custom format of ";;;" without quotes. Then if you protect the sheet checking the hidden box in the cells format the formula won't be displayed in the formula bar. the custom format works despite what colour formats are set in the cell. Adjust the permutations of cell formats to suit

HT makes sense!
 
Upvote 0
Thsi works great but I have only 1 question about it which is:

If I have ran this once and got all my workbook set up correctly how do I then get the User list sheet visible again to add more users !

PS I also want to set this to be visible for me ! The ADMIN )

thanks
 
Upvote 0
I take it that you've locked your self out of the worksheet?

Insert a new module in VBA and put:

Code:
Sub UnhideAll()
    Dim ws As Worksheet
    Application.DisplayAlerts = False
        For Each ws In ActiveWorkbook.Worksheets
            ws.Visible = True
        Next ws
    Application.DisplayAlerts = True
End Sub
Then add yourself to the User list or use the following in the ThisWorkbook module
Code:
Private Sub Workbook_Open()
CurrentUserName = Environ("Username")
        If CurrentUserName = "yourusername" Then
            Exit Sub
    End If
End Sub
Hope that helps,

Smitty
 
Upvote 0
Cheers Pannysaver,

Ive been trying to implement the bottom bit now that I have gotten back into the VBA code and cant actually seem to get the Environment Username part to work.

When you state "add this into the thisworkbook module" do you mean object as it wont let me create a module and rename it to that title. Im presuming that you mean the one that is already there with sheet1 sheet2 and sheet3 when you have a brand new workbook ?

secondly how exactly does the environment part work - I am on a works system that works off our surname then our first initial hence "pricem" or does it actually work from a windows login ( which this might well be im not quite sure ...

thanks for your help thus far ...

Mike
 
Upvote 0

Forum statistics

Threads
1,217,409
Messages
6,136,457
Members
450,013
Latest member
k4kamal

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