Excel

Worksheet Protection Manager For Multiple Users

Ease of Use

Intermediate

Version tested with

2002 

Submitted by:

Jacob Hilderbrand

Description:

Worksheets are automatically unhidden depending on the user logged into the system. Use caution because user MUST enable macros to provide this security. 

Discussion:

You may want to send a workbook with multiple hidden sheets out to various personnel, and only allow them to view their own worksheet. Along with this procedure, you should use a method in your workbook to force the user to enable macros in order to see anything at all. 

Code:

instructions for use

			

'Place this code in the User Form code section. Option Explicit Private Sub Cancel_Click() Unload Me End Sub Private Sub OK_Click() Dim c As Object Dim UserName As String Dim Password As String Dim MyError As Integer 'Assign UserName to a variable UserName = UserVerification.UserName.Text 'Assign Password to a variable Password = UserVerification.Password.Text 'Specify the sheet and range to look up the UserName With Sheets("UserNames").Range("A:A") Set c = .Find(What:=UserName, LookIn:=xlValues, lookat:=xlWhole, MatchCase:=True) End With 'If UserName cannot be found If c Is Nothing Then MsgBox "The username you have inputted is invalid, please try again.", vbCritical, _ "Incorrect UserName" Exit Sub End If 'If Password is correct If Password = Sheets("UserNames").Range(c.Address).Offset(0, 1).Text Then ThisWorkbook.Unprotect Password:="Password" 'If the sheet to make visible is not in the workbook an error will occur 'If there is no error then make that sheet visible On Error Resume Next Sheets(Range(c.Address).Offset(0, 2).Text).Visible = True MyError = Err On Error GoTo 0 ThisWorkbook.Protect Password:="Password", Structure:=True 'If there was an error let the user know to try again. If MyError <> 0 Then MsgBox "The sheet specified could not be shown, please contact your system " & _ "administrator.", vbCritical, "Sheet Specification Error" Exit Sub End If Else 'If the password is incorrect let the user know to try again MsgBox "The password you have specified is incorrect, please try again.", vbCritical, _ "Incorrect Password" Exit Sub End If 'Dismiss the userform Unload Me End Sub 'Place this code in the ThisWorkbook code section. Option Explicit Private Sub Workbook_SheetBeforeRightClick(ByVal Sh As Object, ByVal Target As Range, Cancel As Boolean) Cancel = True If ActiveSheet.Name <> "UserNames" Then ThisWorkbook.Unprotect Password:="Password" ActiveSheet.Visible = xlVeryHidden ThisWorkbook.Protect Password:="Password", Structure:=True End If End Sub

How to use:

  1. Download the attached file for testing, and then emulate it in your own workbook by copying the codes into your own workbook in the same locations.
 

Test the code:

  1. Open the attached zip file and extract the Excel file from it.
  2. Open the Excel file and press the button on the worksheet to call the userform.
  3. You will see example usernames and passwords and sheet names on the only visible worksheet.
  4. Use those to "log into" the userform and the specified worksheet becomes visible.
  5. Right-click on that worksheet to make it hidden again.
 

Sample File:

multiuser.zip 12.92KB 

Approved by mdmackillop


This entry has been viewed 548 times.

Please read our Legal Information and Privacy Policy
Copyright @2004 - 2020 VBA Express