markkuznetsov1
New Member
- Joined
- Sep 23, 2023
- Messages
- 8
- Office Version
- 365
- Platform
- Windows
I am trying to write a code for a sheet that should always be protected for all users upon opening unless you are in an "admin" cell on another sheet called "AuthUsers". The page should open protected for all users and they can only hit a button that has the code to unprotect,run macro, and re protect at the end. This is what I have so far...I know it is wrong but I was trying multiple codes and this is my final. How can I fix it?
Users = Environ("UserName")
Set D = Worksheets("AuthUsers").Range("A3:A4").Find(Users, LookIn:=xlValues, lookat:=xlWhole, MatchCase:=False)
If ThisWorkbook.Path = Environ("UserName") Then ThisWorkbook.Worksheets("EDP #S").Unprotect ("EDPPassword")
Else: If ThisWorkbook.Path <> Environ("UserName") Then ThisWorkbook.Worksheets("EDP #S").Protect ("EDPPassword")
End If
I started from this block and tried to mimic it. (used for read only)
Users = Environ("USERNAME")
Set c = Worksheets("AuthUsers").Range("A1:A100").Find(Users, LookIn:=xlValues, lookat:=xlWhole, MatchCase:=False)
If c Is Nothing Then
Application.DisplayAlerts = False
On Error Resume Next
If ThisWorkbook.Path <> vbNullString Then ThisWorkbook.ChangeFileAccess xlReadOnly
On Error GoTo 0
Application.DisplayAlerts = True
End If
Users = Environ("UserName")
Set D = Worksheets("AuthUsers").Range("A3:A4").Find(Users, LookIn:=xlValues, lookat:=xlWhole, MatchCase:=False)
If ThisWorkbook.Path = Environ("UserName") Then ThisWorkbook.Worksheets("EDP #S").Unprotect ("EDPPassword")
Else: If ThisWorkbook.Path <> Environ("UserName") Then ThisWorkbook.Worksheets("EDP #S").Protect ("EDPPassword")
End If
I started from this block and tried to mimic it. (used for read only)
Users = Environ("USERNAME")
Set c = Worksheets("AuthUsers").Range("A1:A100").Find(Users, LookIn:=xlValues, lookat:=xlWhole, MatchCase:=False)
If c Is Nothing Then
Application.DisplayAlerts = False
On Error Resume Next
If ThisWorkbook.Path <> vbNullString Then ThisWorkbook.ChangeFileAccess xlReadOnly
On Error GoTo 0
Application.DisplayAlerts = True
End If