Excel:password protected sheets

[Closed]
Report
Posts
1
Registration date
Wednesday September 25, 2013
Status
Member
Last seen
September 28, 2013
-
Posts
2779
Registration date
Sunday September 12, 2010
Status
Moderator
Last seen
July 29, 2021
-
Hello,

I hope someone can please help me in adding a password to excel as admin and user..when a username password is entered it will show sheet 1-4 but cannot edit anything and when an admin password was entered it will show all the sheet from 1-7 and can do anything..

thank you in advance..hoping for your time to help me..thanks..

1 reply

Posts
2779
Registration date
Sunday September 12, 2010
Status
Moderator
Last seen
July 29, 2021
468
Hi she27,

Keep in mind that macro's can always be disabled.
You can change both passwords which are the 3rd and 4th codeline.

Here is the code:
Private Sub Workbook_Open()
Dim UserPass, AdminPass As String, sh As Worksheet, x As Integer

UserPass = "Username"
AdminPass = "Admin"

TryAgain:
MyPass = Application.InputBox("Please enter your password:")
If MyPass = False Then
MsgBox "Workbook will now be closed."
ActiveWorkbook.Close
End If

If MyPass = UserPass Then
x = 0
Do
x = x + 1
Sheets(x).Protect DrawingObjects:=True, Contents:=True, Scenarios:=True
Loop Until x = 4

Do
x = x + 1
Sheets(x).Visible = False
Loop Until x = 7

ElseIf MyPass = AdminPass Then
For Each sh In Worksheets
ActiveSheet.Unprotect
Next sh
x = 4
Do
x = x + 1
Sheets(x).Visible = True
Loop Until x = 7

Else
y = MsgBox("The entered password is incorrect.", vbOKCancel)
If y = vbCancel Then
MsgBox "Workbook will now be closed."
ActiveWorkbook.Close
End If
GoTo TryAgain
End If

End Sub
Best regards,
Trowa

Subscribe To Our Newsletter!

The Best of CCM in Your Inbox

Subscribe To Our Newsletter!