Excel with protection based on Windows user name

Tim Ng

New Member
Joined
Jan 11, 2014
Messages
8
I'm thinking of setting below protection for my Excel file so only designated users could open and use it.

The concept is that I'll put a list of Windows user name in the file (in a hidden sheet) which lists out who can open the file.
Then when the file is being opened, it will lookup the Windows user folder location and see any folder name as per Windows user name in hidden sheet is available.
If not, it would either cannot be opened or cleared all contents in the Excel file immediately.

This is a bit complex (or impossible) that I've no idea how to accomplish that...:eek:
Anybody could help in this case? :cool:
 

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
Honestly this is pretty insecure and easily bypassed - you'd be better off requiring a password to open or putting the workbook on a shared drive only those with permission have access to
 
Upvote 0
You should be able to achieve what you need with the below code. However, as Kyle123 said, this can be easily bypassed with anyone who have little experience in excel or with some google search

Code:
Private Sub Workbook_Open()

Dim Check As Boolean, CheckUser As String, PCUser As String, i As Long

Check = False
PCUser = Environ("UserName")

For i = 1 To Sheet1.Range("A" & Rows.Count).End(xlUp).Row ' you need to change the sheet & range based on your file
CheckUser = Sheet1.Range("A" & i).Value
    If PCUser = CheckUser Then
        Check = True
        Exit Sub
    End If
Next i

MsgBox "You don't have permission to access this file", vbCritical
ActiveWorkbook.Close SaveChanges:=False

End Sub
 
Upvote 0
Hi Kyle and MSE330, many thanks for sharing and this really works for me. :)
Regarding to the suggestion - since the file needs to be put in users' computer to use with, it is not possible to put it in restricted share drive.
Also the use of password protect is not that workable as users may share the password to unauthorized users easily.
Anyway I'm quite sure users would not be able to jailbreak the protection code suggested by MSE330 based on my understanding... :LOL:
 
Upvote 0

Forum statistics

Threads
1,215,235
Messages
6,123,786
Members
449,125
Latest member
shreyash11

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