View Single Post
Old 10-14-2021, 03:03 PM
Logit Logit is offline Windows 10 Office 2007
Join Date: Jan 2017
Posts: 405
Logit will become famous soon enoughLogit will become famous soon enough

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
        '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

Private Sub UserForm_Initialize()
    Me.UserName.Value = "User"
End Sub
Attached Files
File Type: xls Multiple User Sheet Protection.xls (46.5 KB, 2 views)
Reply With Quote