Hi There,
I've just quickly thrown together a rough example. If you take a quick peek at the code you will see it's pretty straight forward.
Current users are user1, user2, user3 and user4 and have the passwords, changeme1, changeme2, changeme3 and changeme4 respectively.
user1 has "Admin" rights so if you log in as them you will see all the sheets in the workbook.
hth.
Mod edit: code added below for the benefit of all.
Function checkLogin(ByVal username As String, ByVal password As String) As Boolean
With Sheets("Usernames")
If Application.CountIf(.Range("A:A"), username) > 0 Then
If Application.VLookup(username, .Range("A:B"), 2, False) = password Then
checkLogin = True
Else
checkLogin = False
End If
Else
checkLogin = False
End If
End With
End Function
Function userlevel(ByVal username As String) As String
With Sheets("Usernames")
If Application.CountIf(.Range("A:A"), username) > 0 Then
userlevel = Application.VLookup(username, .Range("A:C"), 3, False)
Else
userlevel = ""
End If
End With
End Function
Sub showSheets(ByVal userlevel As String)
Dim sht As Worksheet
Dim c As Range
With Sheets("Access_Rights").Range("A2", Sheets("Access_Rights").Cells(Sheets("Access_Rights").Rows.Count, 1).End(xlUp))
If .Cells(1, 1).Row > 1 Then
For Each c In .Cells
Set sht = Nothing
If StrComp(c.Value, userlevel, vbTextCompare) = 0 Then
On Error Resume Next
Set sht = Sheets(c(1, 2).Value)
On Error GoTo 0
If Not sht Is Nothing Then sht.Visible = True
End If
Next c
End If
End With
End Sub
Private Sub setUser(ByVal username As String)
Dim sht As Worksheet
If username = "" Then Exit Sub
Application.Names("currentUser").Value = username
If Application.CountIf(Sheets("Usernames").Range("A:A"), username) > 0 Then
On Error Resume Next
Set sht = Sheets(Application.VLookup(username, Sheets("Usernames").Range("A:D"), 4, False))
On Error GoTo 0
If Not sht Is Nothing Then sht.Select
End If
End Sub
Private Sub hideWelcomePage()
Dim sht As Worksheet
For Each sht In Sheets
If Not sht Is Sheets("Welcome") Then
If sht.Visible = xlSheetVisible Then
Sheets("Welcome").Visible = False
Exit For
End If
End If
Next sht
End Sub
Sub logout()
Dim sht As Worksheet
Sheets("Welcome").Visible = True
For Each sht In Sheets
If Not sht Is Sheets("Welcome") Then sht.Visible = xlSheetVeryHidden
Next sht
End Sub
Sub login()
Dim username As String
Dim password As String
username = Application.InputBox("Username", Type:=2)
password = Application.InputBox("Password", Type:=2)
If checkLogin(username, password) Then
showSheets userlevel(username)
setUser username
hideWelcomePage
Else
MsgBox "Incorrect login details", vbCritical
End If
End Sub
Bookmarks