0

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.

4
  • Yes you can, and you might have to use VBA.Environ("UserName") to make it work on some computers! ;) Commented Nov 25, 2015 at 15:31
  • 2
    this is not security. You should only even consider it if it is for a convenience (i.e. hiding not relevant info). Commented Nov 25, 2015 at 15:39
  • yes this is not really for security. This is just to make sure that the users will only see fewer worksheets. Commented Nov 25, 2015 at 15:48
  • BTW, when I user environ("username") here, it will only show the employee ID which is alphanumeric and not really the name of the employee. So can you give me an example? let say the ID of one employee is ABC123 and I only want him to see worksheet named as sheet1 and sheet2. Then for employee DEF456 it will only show sheet3 and sheet4 Commented Nov 25, 2015 at 15:51

1 Answer 1

2

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_Open event can be easily bypassed by holding the shift button when opening the file.
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.