I am using a login form to customize which sheets to display based on who logs in. It works fine (though I know it's not the cleanest code) but if another workbook is open, I get "Subscript out of range." It appears to me that it is trying to find worksheets in the already open workbook instead of the one that is being logged into.
Instead of telling users they have to close all open workbooks before opening this one, is there a way to handle this error?
Code is below:
Private Sub cmdLogin_Click()
Dim user As String
Dim password As String
user = Me.txtUserID.Value
password = Me.txtPassword.Value
If (user = "admin" And password = "mon57701") Or (user = "rcsales" And password = "lifesong") Or (user = "showroom" And password = "update") Then
'THIS SECTION IS USED TO CUSTOMIZED LOGIN
If user = "rcsales" Then
Unload Me
Worksheets("LifeSong Report").Visible = False
Worksheets("RileyStatus2021").Visible = True
Worksheets("RileyStatus2021").Protect
Worksheets("RileySort").Visible = False
Worksheets("RileySales").Visible = True
Worksheets("RileySales").Protect
Worksheets("NCOCalc").Visible = False
Worksheets("COMMRATE").Visible = False
Worksheets("SFSales").Visible = False
Worksheets("Targets").Visible = True
Worksheets("Targets").Protect
Application.Visible = True
frmAdminMenu.Show
ElseIf user = "admin" Then
Unload Me
Worksheets("LifeSong Report").Visible = True
Worksheets("RileyStatus2021").Visible = True
Worksheets("RileyStatus2021").Unprotect
Worksheets("RileySort").Visible = False
Worksheets("RileySales").Visible = True
Worksheets("RileySales").Unprotect
Worksheets("NCOCalc").Visible = True
Worksheets("COMMRATE").Visible = False
Worksheets("SFSales").Visible = True
Worksheets("Targets").Visible = True
Worksheets("Targets").Unprotect
Application.Visible = True
frmAdminMenu.Show
Else
Unload Me
Worksheets("LifeSong Report").Visible = False
Worksheets("RileyStatus2021").Visible = False
Worksheets("RileyStatus2021").Protect
Worksheets("RileySort").Visible = False
Worksheets("RileySales").Visible = True
Worksheets("RileySales").Unprotect
Worksheets("NCOCalc").Visible = False
Worksheets("COMMRATE").Visible = False
Worksheets("SFSales").Visible = False
Worksheets("Targets").Visible = False
Worksheets("Targets").Protect
Application.Visible = True
frmAdminMenu.Show
End If
Bookmarks