Excel:password protected sheets

Closed
she27 Posts 1 Registration date Wednesday September 25, 2013 Status Member Last seen September 28, 2013 - Sep 28, 2013 at 01:12 AM
TrowaD Posts 2921 Registration date Sunday September 12, 2010 Status Moderator Last seen December 27, 2022 - Sep 30, 2013 at 11:55 AM
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..
Related:

1 response

TrowaD Posts 2921 Registration date Sunday September 12, 2010 Status Moderator Last seen December 27, 2022 552
Sep 30, 2013 at 11:55 AM
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
0