First use this code to poll your network:
Sub mySys()
Dim DomainName, ComputerName, UserName
DomainName = Environ("UserDomain")
ComputerName = Environ("ComputerName")
UserName = Environ("UserName")
MsgBox "Network Domain Name: ==> " & DomainName & Chr(10) & _
"Full Comuter Name: ==> " & ComputerName & Chr(10) & _
"User Name: ==> " & UserName & Chr(10) & _
Chr(10) & "Application User Name: " & Application.UserName
End Sub
This is a simple list of the system ID Info. this will let you see what kind and what format that info has!
Then you can pick the right info to test with, in this next test I used: "Application.UserName" to work with you may need to change this to one of the others returned by the 1st Sub above?
Sub myUser()
'Show User Name!
If Application.UserName = "" Then GoTo NoNam Else GoTo HNam
End
NoNam:
'User name not in system.
'Get User Name as input.
UNText = Application.InputBox( _
prompt:="The user: " & UserName & " has not indicated their name," & Chr(13) & _
"in the Tools-Options-General, User Name: box of Excel." & _
Chr(13) & "Or the network system User Name was not found?" & _
Chr(13) & Chr(13) & _
"Please enter the User Name here:" & Chr(13) & Chr(13) & _
"Note: This will not change the User Name in Options!", _
Title:="User Name not installed on this PC!")
HNam:
MsgBox "Environment User Name: " & Environ("username") & Chr(13) & _
Chr(13) & "Application User Name: " & Application.UserName
End Sub
This code checks to see if the users ID is in the system, more correctly it checks to see if the PC being used has been registered!
Once you get which ID to use as your KEY worked out, Add code like the "myUser" Sub above to a: ThisWorkbook module Open Event with a "Select Case" structure to control which sheets that user can view:
Public thisUserN$
'Note: the Public Var above should be the
'1st entery at the top of the
'ThisWorkbook module!
Private Sub Workbook_Open()
'ThisWorkbook module code!
'Show User Name!
Dim sh As Worksheet
If Application.UserName = "" Then GoTo NoNam Else GoTo HNam
End
NoNam:
'User name not in system.
'Get User Name as input.
UNText = Application.InputBox( _
prompt:="The user: " & UserName & " has not indicated their name," & Chr(13) & _
"in the Tools-Options-General, User Name: box of Excel." & _
Chr(13) & "Or the network system User Name was not found?" & _
Chr(13) & Chr(13) & _
"Please enter the User Name here:" & Chr(13) & Chr(13) & _
"Note: This will not change the User Name in Options!", _
Title:="User Name not installed on this PC!")
HNam:
thisUserN = Application.UserName
Select Case thisUserN
Case "JoeWas"
'Authorize user: JoeWas for Sheet2 only!
For Each sh In Worksheets
If sh.Name <> "Sheet2" Then
sh.Visible = False
Else
sh.Visible = True
End If
Next sh
Case "NextUsersID"
'Authorize user: NextUsersID for Sheet3 only!
For Each sh In Worksheets
If sh.Name <> "Sheet3" Then
sh.Visible = False
Else
sh.Visible = True
End If
Next sh
Case "xxxxxx"
'Authorize user: BackDoor code xxxxxx for Sheet4 only!
For Each sh In Worksheets
If sh.Name <> "Sheet4" Then
sh.Visible = False
Else
sh.Visible = True
End If
Next sh
Case Else
'Authorize user: Not Found! for No sheets!
For Each sh In Worksheets
If sh.Name <> "Sheet1" Then
sh.Visible = False
Else
sh.Visible = True
End If
Next sh
MsgBox "You are not authorized to use this application!"
'Option: Exit Application, un-comment below!
'Application.Quit
End Select
End Sub
Just add as many Sheets and Case Statements as you need, and you are done!
Note: The Public Var: thisUserN can be used by any Sub in any Module as needed!
Now lets say you have 10,000 users, the Select Case structure will not be practical so don't use the Select Case Structure: Name each Users Sheet the name returned by the system!
And, only use:
For Each sh In Worksheets
If sh.Name <> thisUserN Then
sh.Visible = False
Else
sh.Visible = True
End If
Next sh
So, this one code block will work for every registered User!
As in this case our Public Var: thisUserN = Application.UserName
Which also happens to be the name of that users Sheet!
You can modify the code to also include Sheets everyone can view by changing:
If sh.Name <> thisUserN Then
To:
If (sh.Name <> "Sheet1" Or sh.Name <> thisUserN) Then
Now lets say you have all you users on one WorkSheet?
Name each users data range with a Range Name then Range Name the "common to all" range(s). Change the code to: Hide all but the indicated Ranges!
To protect your users from getting into your code and seeing what you used to keep them out, see this link:
http://www.mrexcel.com/board2/viewtopic.php?t=172763