I just want to ask if it's possible to create a VBA that detects the identity of the person opening the workbook using Environ$("username") and using that I can hide some of the worksheet and unhide other worksheet that only that person can see?. Then if another person opens up the same workbook it will then hide/unhide worksheets for that other person.
1 Answer
Example of what you want (for educational purposes only):
Private Sub Workbook_Open()
Select Case Environ("username")
Case "bloggsj"
Sheets(1).Visible = False
Sheets(2).Visible = True
Case "doej", "murphyp", "manm"
Sheets(2).Visible = True
Sheets(1).Visible = False
Case Else
'// Unknown, close workbook
ThisWorkbook.Close False
End Select
End Sub
Important: As already mentioned in the comments, this is not a secure way of protecting data, two quick reasons as an example:
- The Environment variable "username" can easily be changed which would trick the code into thinking that the person is a different user.
- The
Workbook_Openevent can be easily bypassed by holding the shift button when opening the file.
VBA.Environ("UserName")to make it work on some computers! ;)