unprotect sheet based on user

wmtsub

Active Member
Joined
Jun 20, 2018
Messages
322
Is ther anyway I cn automate a spredsheet to unprotect based on teh profile name of the system user so there is no actual user interaction?
 

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
Hi

You can copy the code in 'This Workbook' Object.

Private Sub Workbook_Open()


Dim UserNames As String




ActiveWorkbook.Sheets("Sheet1").Unprotect Password:="***"




UserNames = Application.UserName




Select Case UserNames


Case "Saba Sabaratnam"


Case "wmtsub"




Case Else


ActiveWorkbook.Sheets("Sheet1").Protect Password:="***"


End Select




End Sub


When your workbook is opened, it will compare system user name with the names in Select Case and unprotect sheet 1 if it finds matching name. Otherwise, it will protect the sheet. You can include other names against.

You may want to protect the code by going Tool - VBA Project Properties - Protection on VBA editor and entering a password for the code. Otherwise, one user can change the code to include other names or change the worksheet password.

Kind regards

Saba
 
Upvote 0
If you want the system user name (ie windows logon name) rather the the excel name, change
Code:
UserNames = Application.UserName
to
Code:
UserNames = environ("UserName")
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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